开发者

Converting numeric value with currency symbol back to Decimal with NumberFormat

I would like to convert a possibly Decimal value prefixed with currency symbol into only numeric value.

For example -

The value can be like any of the following

String s1 = "£32,847,676.65";
String s2 = "£3,456.00";
String s3 = "£831,209";

I would like the result after conversion to be like - 32847676.65, 3456.00 and 831209.

I tried using the parse() method of the NumberFormat in this way -

NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.UK);
numberFormat.setMinimumFractionDigits(2);
Number num = nf.parse(s1);
double dd = num.doubleValue();
BigDecimal gg = new BigDecimal(dd);
Syst开发者_StackOverflowem.out.println(gg);

But the result is - 32847676.649999998509883880615234375 which is not quite exactly the correct one.

I need it to be numeric so that may be I can perform some kind of calculation.

Can you guys guide me with what else can I try


You already parse the value correctly. The problem is this:

BigDecimal gg = new BigDecimal(dd);

You covnert the value to BigDecimal, and the rounding problems of doubles account for the decimal places after the dot. Use:

BigDecimal gg = new BigDecimal(dd).setScale(2);

or

BigDecimal gg = new BigDecimal(dd).setScale(2,RoundingMode.HALF_UP);


When playing with BigDecimal, the appropriate constructor is BigDecimal(String val)

    NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.UK);
    BigDecimal gg = new BigDecimal(nf.parse(s1).toString());
    System.out.println(gg);

BigDecimal(double val) does construct an exact decimal representation of the double value, which is not the human readable value you expected.

"The results of this constructor can be somewhat unpredictable. One might assume that writing new BigDecimal(0.1) in Java creates a BigDecimal which is exactly equal to 0.1 (an unscaled value of 1, with a scale of 1), but it is actually equal to 0.1000000000000000055511151231257827021181583404541015625. This is because 0.1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the value that is being passed in to the constructor is not exactly equal to 0.1, appearances notwithstanding. [...] Therefore, it is generally recommended that the String constructor be used in preference to this one"

Source : BigDecimal javadoc


You can try the following without BigDecimal or NumberFormat. ;)

String s1 = "£32,847,676.65";
// remove the £ and ,
String s2 = s1.replaceAll("[£,]", "");
// then turn into a double 
double d = Double.parseDouble(s2);
// and round up to two decimal places.
double value = (long) (d * 100 + 0.5) / 100.0;

System.out.printf("%.2f%n", value);

prints

32847676.65

If youw ant to avoid rounding error in your calculations but don't want the heavy weight BigDecimal you can use long cents.

// value in cents as an integer.
long value = (long) (d * 100 + 0.5);

// perform some calculations on value here

System.out.printf("%.2f%n", value / 100.0);


It is not guaranteed to work, but according to the NumberFormat API documentation, its getXyzInstance methods will return a DecimalFormat instance for "the vast majority of locales". This can probably be interpreted as "for all locales, unless proprietary locale service providers are installed".

If you can cast your NumberFormat to DecimalFormat, you can tell it to parse to a BigDecimal directly, reducing your code to:

DecimalFormat nf = (DecimalFormat) NumberFormat.getCurrencyInstance(Locale.UK);
nf.setParseBigDecimal(true);
BigDecimal gg = (BigDecimal) nf.parse(s1);
System.out.println(gg);

In this case, you will have no problem with the inaccuracy of binary floating point numbers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜