mirror of
https://github.com/hyperdefined/ToolStats.git
synced 2025-12-10 22:55:04 +00:00
@@ -17,21 +17,90 @@
|
||||
|
||||
package lol.hyper.toolstats.tools;
|
||||
|
||||
import lol.hyper.toolstats.ToolStats;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.DecimalFormatSymbols;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
public class NumberFormat {
|
||||
|
||||
private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#,###.00", new DecimalFormatSymbols(Locale.getDefault()));
|
||||
private static final DecimalFormat COMMA_FORMAT = new DecimalFormat("#,###", new DecimalFormatSymbols(Locale.getDefault()));
|
||||
private DecimalFormat DECIMAL_FORMAT;
|
||||
private DecimalFormat COMMA_FORMAT;
|
||||
private SimpleDateFormat DATE_FORMAT;
|
||||
|
||||
public NumberFormat(ToolStats toolStats) {
|
||||
|
||||
String dateFormat = toolStats.config.getString("date-format");
|
||||
String decimalSeparator = toolStats.config.getString("number-formats.decimal-separator");
|
||||
String commaSeparator = toolStats.config.getString("number-formats.comma-separator");
|
||||
String commaFormat = toolStats.config.getString("number-formats.comma-format");
|
||||
String decimalFormat = toolStats.config.getString("number-formats.decimal-format");
|
||||
|
||||
if (dateFormat == null) {
|
||||
dateFormat = "M/dd/yyyy";
|
||||
toolStats.logger.warning("date-format is missing! Using default American English format.");
|
||||
}
|
||||
|
||||
if (decimalSeparator == null) {
|
||||
decimalSeparator = ".";
|
||||
toolStats.logger.warning("number-formats.decimal-separator is missing! Using default \".\" instead.");
|
||||
}
|
||||
|
||||
if (commaSeparator == null) {
|
||||
commaSeparator = ",";
|
||||
toolStats.logger.warning("number-formats.comma-separator is missing! Using default \",\" instead.");
|
||||
}
|
||||
|
||||
if (commaFormat == null) {
|
||||
commaFormat = "#,###";
|
||||
toolStats.logger.warning("number-formats.comma-format is missing! Using default #,### instead.");
|
||||
}
|
||||
|
||||
if (decimalFormat == null) {
|
||||
decimalFormat = "#,###.00";
|
||||
toolStats.logger.warning("number-formats.comma-separator is missing! Using default #,###.00 instead.");
|
||||
}
|
||||
|
||||
try {
|
||||
DATE_FORMAT = new SimpleDateFormat(dateFormat, Locale.getDefault());
|
||||
} catch (NullPointerException | IllegalArgumentException exception) {
|
||||
toolStats.logger.warning("date-format is NOT a valid format! Using default American English format.");
|
||||
exception.printStackTrace();
|
||||
DATE_FORMAT = new SimpleDateFormat("M/dd/yyyy", Locale.ENGLISH);
|
||||
}
|
||||
|
||||
DecimalFormatSymbols formatSymbols = new DecimalFormatSymbols(Locale.getDefault());
|
||||
formatSymbols.setDecimalSeparator(decimalSeparator.charAt(0));
|
||||
formatSymbols.setGroupingSeparator(commaSeparator.charAt(0));
|
||||
|
||||
|
||||
try {
|
||||
COMMA_FORMAT = new DecimalFormat(commaFormat, formatSymbols);
|
||||
} catch (NullPointerException | IllegalArgumentException exception) {
|
||||
toolStats.logger.warning("number-formats.comma-format is NOT a valid format! Using default #,### instead.");
|
||||
exception.printStackTrace();
|
||||
COMMA_FORMAT = new DecimalFormat("#,###", formatSymbols);
|
||||
}
|
||||
|
||||
try {
|
||||
DECIMAL_FORMAT = new DecimalFormat(decimalFormat, formatSymbols);
|
||||
} catch (NullPointerException | IllegalArgumentException exception) {
|
||||
toolStats.logger.warning("number-formats.decimal-format is NOT a valid format! Using default #,###.00 instead.");
|
||||
exception.printStackTrace();
|
||||
DECIMAL_FORMAT = new DecimalFormat("#,###.00", formatSymbols);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a number to make it pretty. Example: 4322 to 4,322
|
||||
*
|
||||
* @param number The number to format.
|
||||
* @return The formatted number.
|
||||
*/
|
||||
public static String formatInt(int number) {
|
||||
public String formatInt(int number) {
|
||||
String finalNumber = COMMA_FORMAT.format(number);
|
||||
finalNumber = finalNumber.replaceAll("[\\x{202f}\\x{00A0}]", " ");
|
||||
return finalNumber;
|
||||
@@ -39,12 +108,23 @@ public class NumberFormat {
|
||||
|
||||
/**
|
||||
* Formats a number to make it pretty. Example: 4322.33 to 4,322.33
|
||||
*
|
||||
* @param number The number to format.
|
||||
* @return The formatted number.
|
||||
*/
|
||||
public static String formatDouble(double number) {
|
||||
public String formatDouble(double number) {
|
||||
String finalNumber = DECIMAL_FORMAT.format(number);
|
||||
finalNumber = finalNumber.replaceAll("[\\x{202f}\\x{00A0}]", " ");
|
||||
return finalNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a date into the readable format.
|
||||
*
|
||||
* @param date The date to format.
|
||||
* @return The date into a readable format.
|
||||
*/
|
||||
public String formatDate(Date date) {
|
||||
return DATE_FORMAT.format(date);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user