| Construct BigDecimal from a localised String |
|
|
|
| Written by Charles | |||||
| Saturday, 14 November 2009 17:10 | |||||
|
It would appear that BigDecimal doesn't accept localised strings in its principal constructor and that the dot is used as the decimal separator. Grouping characters are not supported at all. The following workaround can be used and the code is HERE import java.util.*;
import java.text.*; import java.math.BigDecimal; public class FormatBigDecimal { public static void main(String[] args) { Locale.setDefault(new Locale("nl", "NL")); String nl_NL_BigDecimal = "343.298.723.948.729.384.091.826.095.842.735.093,09324798"; //This is a valid number in nl_NL... BigDecimal bLoc = getBigDecimalLocalised(nl_NL_BigDecimal, Locale.getDefault()); System.out.println(bLoc); } public static BigDecimal getBigDecimalLocalised(String value, Locale locale) { DecimalFormatSymbols dfs = new DecimalFormatSymbols(locale); char sep = dfs.getDecimalSeparator(); char gp = dfs.getGroupingSeparator(); StringBuilder sb = new StringBuilder(value); for(int i = sb.length() - 1;i >=0;i--) { if (sb.charAt(i) == gp) { sb.deleteCharAt(i); } else if (sb.charAt(i) == sep) { sb.setCharAt(i, '.'); } } return new BigDecimal(sb.toString()); } }
Only registered users can write comments!
Powered by !JoomlaComment 3.26
3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved." |
|||||
| Last Updated ( Saturday, 14 November 2009 17:24 ) |



