2013-06-08 21:31:19 +00:00
|
|
|
package com.earth2me.essentials.utils;
|
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
import net.ess3.api.IEssentials;
|
|
|
|
|
2013-06-08 21:31:19 +00:00
|
|
|
import java.math.BigDecimal;
|
|
|
|
import java.math.RoundingMode;
|
|
|
|
import java.text.DecimalFormat;
|
|
|
|
import java.text.DecimalFormatSymbols;
|
2016-01-13 22:54:00 +00:00
|
|
|
import java.text.NumberFormat;
|
2013-06-08 21:31:19 +00:00
|
|
|
import java.util.Locale;
|
2015-04-15 04:06:16 +00:00
|
|
|
|
|
|
|
import static com.earth2me.essentials.I18n.tl;
|
2013-06-08 21:31:19 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
public class NumberUtil {
|
2018-03-21 10:16:16 +00:00
|
|
|
|
2020-04-25 12:08:57 +00:00
|
|
|
private static final DecimalFormat twoDPlaces = new DecimalFormat("#,###.##");
|
|
|
|
private static final DecimalFormat currencyFormat = new DecimalFormat("#0.00", DecimalFormatSymbols.getInstance(Locale.US));
|
2016-06-19 19:03:54 +00:00
|
|
|
|
|
|
|
// This field is likely to be modified in com.earth2me.essentials.Settings when loading currency format.
|
|
|
|
// This ensures that we can supply a constant formatting.
|
2018-03-13 15:20:02 +00:00
|
|
|
private static NumberFormat PRETTY_FORMAT = NumberFormat.getInstance(Locale.US);
|
2016-01-13 22:54:00 +00:00
|
|
|
|
|
|
|
static {
|
|
|
|
twoDPlaces.setRoundingMode(RoundingMode.HALF_UP);
|
|
|
|
currencyFormat.setRoundingMode(RoundingMode.FLOOR);
|
|
|
|
|
|
|
|
PRETTY_FORMAT.setRoundingMode(RoundingMode.FLOOR);
|
|
|
|
PRETTY_FORMAT.setGroupingUsed(true);
|
|
|
|
PRETTY_FORMAT.setMinimumFractionDigits(2);
|
|
|
|
PRETTY_FORMAT.setMaximumFractionDigits(2);
|
|
|
|
}
|
2015-04-15 04:06:16 +00:00
|
|
|
|
2018-03-21 10:16:16 +00:00
|
|
|
// this method should only be called by Essentials
|
|
|
|
public static void internalSetPrettyFormat(NumberFormat prettyFormat) {
|
|
|
|
PRETTY_FORMAT = prettyFormat;
|
|
|
|
}
|
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
public static String shortCurrency(final BigDecimal value, final IEssentials ess) {
|
2020-04-25 12:12:55 +00:00
|
|
|
if (ess.getSettings().isCurrencySymbolSuffixed()) {
|
|
|
|
return formatAsCurrency(value) + ess.getSettings().getCurrencySymbol();
|
|
|
|
}
|
2015-04-15 04:06:16 +00:00
|
|
|
return ess.getSettings().getCurrencySymbol() + formatAsCurrency(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String formatDouble(final double value) {
|
|
|
|
return twoDPlaces.format(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String formatAsCurrency(final BigDecimal value) {
|
|
|
|
String str = currencyFormat.format(value);
|
|
|
|
if (str.endsWith(".00")) {
|
|
|
|
str = str.substring(0, str.length() - 3);
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2016-01-13 22:54:00 +00:00
|
|
|
public static String formatAsPrettyCurrency(BigDecimal value) {
|
|
|
|
String str = PRETTY_FORMAT.format(value);
|
|
|
|
if (str.endsWith(".00")) {
|
|
|
|
str = str.substring(0, str.length() - 3);
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
public static String displayCurrency(final BigDecimal value, final IEssentials ess) {
|
2017-08-19 19:11:21 +00:00
|
|
|
String currency = formatAsPrettyCurrency(value);
|
|
|
|
String sign = "";
|
|
|
|
if (value.signum() < 0) {
|
|
|
|
currency = currency.substring(1);
|
|
|
|
sign = "-";
|
|
|
|
}
|
2020-04-25 12:12:55 +00:00
|
|
|
if (ess.getSettings().isCurrencySymbolSuffixed()) {
|
|
|
|
return sign + tl("currency", currency, ess.getSettings().getCurrencySymbol());
|
|
|
|
}
|
2017-08-19 19:11:21 +00:00
|
|
|
return sign + tl("currency", ess.getSettings().getCurrencySymbol(), currency);
|
2015-04-15 04:06:16 +00:00
|
|
|
}
|
|
|
|
|
2015-11-07 17:23:40 +00:00
|
|
|
public static String displayCurrencyExactly(final BigDecimal value, final IEssentials ess) {
|
2017-08-19 19:11:21 +00:00
|
|
|
String currency = value.toPlainString();
|
|
|
|
String sign = "";
|
|
|
|
if (value.signum() < 0) {
|
|
|
|
currency = currency.substring(1);
|
|
|
|
sign = "-";
|
|
|
|
}
|
2020-04-25 12:12:55 +00:00
|
|
|
if (ess.getSettings().isCurrencySymbolSuffixed()) {
|
|
|
|
return sign + tl("currency", currency, ess.getSettings().getCurrencySymbol());
|
|
|
|
}
|
2017-08-19 19:11:21 +00:00
|
|
|
return sign + tl("currency", ess.getSettings().getCurrencySymbol(), currency);
|
2015-11-07 17:23:40 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 14:20:00 +00:00
|
|
|
public static String sanitizeCurrencyString(final String input, final IEssentials ess) {
|
|
|
|
String symbol = ess.getSettings().getCurrencySymbol();
|
|
|
|
boolean suffix = ess.getSettings().isCurrencySymbolSuffixed();
|
|
|
|
if (input.contains(symbol)) {
|
|
|
|
return suffix ? input.substring(0, input.indexOf(symbol)) : input.substring(symbol.length());
|
|
|
|
}
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
public static boolean isInt(final String sInt) {
|
|
|
|
try {
|
|
|
|
Integer.parseInt(sInt);
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2013-06-08 21:31:19 +00:00
|
|
|
}
|