mirror of
https://github.com/TotalFreedomMC/TF-EssentialsX.git
synced 2025-04-25 16:09:44 +00:00
Prettify currency display further.
This commit is contained in:
parent
d570570394
commit
419d2d8282
2 changed files with 22 additions and 4 deletions
|
@ -6,6 +6,7 @@ import java.math.BigDecimal;
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
import java.text.DecimalFormat;
|
import java.text.DecimalFormat;
|
||||||
import java.text.DecimalFormatSymbols;
|
import java.text.DecimalFormatSymbols;
|
||||||
|
import java.text.NumberFormat;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
import static com.earth2me.essentials.I18n.tl;
|
import static com.earth2me.essentials.I18n.tl;
|
||||||
|
@ -14,18 +15,27 @@ import static com.earth2me.essentials.I18n.tl;
|
||||||
public class NumberUtil {
|
public class NumberUtil {
|
||||||
static DecimalFormat twoDPlaces = new DecimalFormat("#,###.##");
|
static DecimalFormat twoDPlaces = new DecimalFormat("#,###.##");
|
||||||
static DecimalFormat currencyFormat = new DecimalFormat("#0.00", DecimalFormatSymbols.getInstance(Locale.US));
|
static DecimalFormat currencyFormat = new DecimalFormat("#0.00", DecimalFormatSymbols.getInstance(Locale.US));
|
||||||
|
static final NumberFormat PRETTY_FORMAT = NumberFormat.getInstance(Locale.US);
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
public static String shortCurrency(final BigDecimal value, final IEssentials ess) {
|
public static String shortCurrency(final BigDecimal value, final IEssentials ess) {
|
||||||
return ess.getSettings().getCurrencySymbol() + formatAsCurrency(value);
|
return ess.getSettings().getCurrencySymbol() + formatAsCurrency(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String formatDouble(final double value) {
|
public static String formatDouble(final double value) {
|
||||||
twoDPlaces.setRoundingMode(RoundingMode.HALF_UP);
|
|
||||||
return twoDPlaces.format(value);
|
return twoDPlaces.format(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String formatAsCurrency(final BigDecimal value) {
|
public static String formatAsCurrency(final BigDecimal value) {
|
||||||
currencyFormat.setRoundingMode(RoundingMode.FLOOR);
|
|
||||||
String str = currencyFormat.format(value);
|
String str = currencyFormat.format(value);
|
||||||
if (str.endsWith(".00")) {
|
if (str.endsWith(".00")) {
|
||||||
str = str.substring(0, str.length() - 3);
|
str = str.substring(0, str.length() - 3);
|
||||||
|
@ -33,8 +43,16 @@ public class NumberUtil {
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
public static String displayCurrency(final BigDecimal value, final IEssentials ess) {
|
public static String displayCurrency(final BigDecimal value, final IEssentials ess) {
|
||||||
return tl("currency", ess.getSettings().getCurrencySymbol(), formatAsCurrency(value));
|
return tl("currency", ess.getSettings().getCurrencySymbol(), formatAsPrettyCurrency(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String displayCurrencyExactly(final BigDecimal value, final IEssentials ess) {
|
public static String displayCurrencyExactly(final BigDecimal value, final IEssentials ess) {
|
||||||
|
|
|
@ -69,7 +69,7 @@ public class EconomyTest extends TestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
//test Format
|
//test Format
|
||||||
assertEquals("Format $1000", "$1000", Economy.format(1000.0));
|
assertEquals("Format $1,000", "$1,000", Economy.format(1000.0));
|
||||||
assertEquals("Format $10", "$10", Economy.format(10.0));
|
assertEquals("Format $10", "$10", Economy.format(10.0));
|
||||||
assertEquals("Format $10.10", "$10.10", Economy.format(10.10));
|
assertEquals("Format $10.10", "$10.10", Economy.format(10.10));
|
||||||
assertEquals("Format $10.10", "$10.10", Economy.format(10.1000001));
|
assertEquals("Format $10.10", "$10.10", Economy.format(10.1000001));
|
||||||
|
|
Loading…
Reference in a new issue