diff options
author | Daniel <daniel112092@gmail.com> | 2022-02-06 18:50:30 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-06 18:50:30 +0100 |
commit | 329cbf885c7a67992903aa8897f9fa23e83d7596 (patch) | |
tree | 089d3f740ce77486a235aefcdd3947e7d20c473b /src | |
parent | 2f85ea82f372efd8fae5ebff6c9f0bb81f2f63aa (diff) | |
download | GT5-Unofficial-329cbf885c7a67992903aa8897f9fa23e83d7596.tar.gz GT5-Unofficial-329cbf885c7a67992903aa8897f9fa23e83d7596.tar.bz2 GT5-Unofficial-329cbf885c7a67992903aa8897f9fa23e83d7596.zip |
Provide a runtime solution for number rendering. (#922)
* Provide a runtime solution for number rendering.
This should adapt based on locale selected by user instead of the system locale.
Also fixes 'nbsp' issue in some locales like pl_PL.
* Add missing imports
* Add missing imports
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/gregtech/api/util/GT_Utility.java | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/src/main/java/gregtech/api/util/GT_Utility.java b/src/main/java/gregtech/api/util/GT_Utility.java index b4df21ae0b..696d0bbf51 100644 --- a/src/main/java/gregtech/api/util/GT_Utility.java +++ b/src/main/java/gregtech/api/util/GT_Utility.java @@ -105,7 +105,9 @@ import javax.annotation.Nullable; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; -import java.text.NumberFormat; +import java.math.RoundingMode; +import java.text.DecimalFormat; +import java.text.DecimalFormatSymbols; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -115,6 +117,7 @@ import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Map.Entry; import java.util.Objects; @@ -143,7 +146,7 @@ import static gregtech.common.GT_UndergroundOil.undergroundOilReadInformation; */ public class GT_Utility { /** Formats a number with group separator and at most 2 fraction digits. */ - private static final NumberFormat numberFormat = NumberFormat.getInstance(); + private static final Map<Locale, DecimalFormat> decimalFormatters=new HashMap<>(); /** * Forge screwed the Fluid Registry up again, so I make my own, which is also much more efficient than the stupid Stuff over there. @@ -162,8 +165,6 @@ public class GT_Utility { public static UUID defaultUuid = null; // maybe default non-null? UUID.fromString("00000000-0000-0000-0000-000000000000"); static { - numberFormat.setMaximumFractionDigits(2); - GregTech_API.sItemStackMappings.add(sFilledContainerToData); GregTech_API.sItemStackMappings.add(sEmptyContainerToFluidToData); @@ -2377,13 +2378,26 @@ public class GT_Utility { } return -1; } + + private static DecimalFormat getDecimalFormat(){ + return decimalFormatters.computeIfAbsent(Locale.getDefault(Locale.Category.FORMAT), locale -> { + DecimalFormat numberFormat =new DecimalFormat();//uses the necessary locale inside anyway + numberFormat.setGroupingUsed(true); + numberFormat.setMaximumFractionDigits(2); + numberFormat.setRoundingMode(RoundingMode.HALF_UP); + DecimalFormatSymbols decimalFormatSymbols = numberFormat.getDecimalFormatSymbols(); + decimalFormatSymbols.setGroupingSeparator(' ');//use international separator for best clarity, should also fix locales using NBSP + numberFormat.setDecimalFormatSymbols(decimalFormatSymbols); + return numberFormat; + }); + } public static String formatNumbers(long aNumber) { - return numberFormat.format(aNumber); + return getDecimalFormat().format(aNumber); } public static String formatNumbers(double aNumber) { - return numberFormat.format(aNumber); + return getDecimalFormat().format(aNumber); } /* |