Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting numeric value with currency symbol back to Decimal with NumberFormat

Tags:

java

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);
System.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

like image 612
Swift-Tuttle Avatar asked Mar 30 '11 10:03

Swift-Tuttle


People also ask

How do I change the currency symbol in accounting number format?

On the Home tab, in the Number group, click Accounting Number Format . If you want to show a currency symbol other than the default, click the arrow next to the Accounting Number Format button and then select another currency symbol.

How to display a number in its country's currency format?

In this article we will learn how to display a number in its currency format with respect to a country. When we want to display a number in its respective country's currency format, we format the string in the currency format and get the currency symbol of a specific country using the "CultureInfo" class available in .Net.

How to format a number into a currency string in Java?

Here's how you can easily format a number, such as a double into a currency String: Formatting numbers in different currencies involves a few Java classes. Our example made use of the Locale and Currency classes from the java.util package. Additionally, we used the NumberFormat class, which can be found in the java.text package.

How do I format numbers as monetary values in Excel?

If you want to display numbers as monetary values, you must format those numbers as currency. To do this, you apply either the Currency or Accounting number format to the cells that you want to format. The number formatting options are available on the Home tab, in the Number group. What's the difference between the Currency and Accounting formats?


2 Answers

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);
like image 126
Daniel Avatar answered Oct 13 '22 04:10

Daniel


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

like image 5
Tanguy Avatar answered Oct 13 '22 04:10

Tanguy