Created
September 13, 2017 14:55
-
-
Save ppazos/9dfb8b856ddd14518de0a4111231a095 to your computer and use it in GitHub Desktop.
Parsing numbers in Java/Groovy considering locale symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def ss = ["12345", "3.14159", "-200.5", "37,1", "1,111,111.55"] | |
def format = java.text.NumberFormat.getInstance() | |
println format.class // DecimalFormat | |
/* | |
12345 class java.lang.Long | |
3.14159 class java.lang.Double | |
-200.5 class java.lang.Double | |
371 class java.lang.Long <<< wrong number for locale, decimal point should be transformed | |
1111111.55 class java.lang.Double | |
*/ | |
ss.each { | |
println format.parse(it) +' '+ format.parse(it).class | |
} | |
def dfs = format.getDecimalFormatSymbols() | |
println dfs.getDecimalSeparator() // . | |
println dfs.getExponentSeparator() // E | |
println dfs.getGroupingSeparator() // , | |
println dfs.getDigit() // # | |
println dfs.getCurrencySymbol() // $ | |
println dfs.getInternationalCurrencySymbol() // USD | |
println dfs.getMonetaryDecimalSeparator() // . | |
println dfs.getPatternSeparator() // ; | |
println dfs.getPercent() // % | |
println java.text.NumberFormat.getInstance().getAvailableLocales() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment