vengatesh
vengatesh

Reputation: 503

Flex Currencyformatter automatically rounds off the larger values

We are working with Amounts of which value are higher. We are displaying the formatted amount in the respective spark TextInput. We are using the simple mx CurrencyFormatter for formatting the amount values. We dont have any problems till 16 digits . But after crossing 16 digits , the numbers are automtically rounded off. We are using the CurrencyFormatter with the following configurations,

<mx:CurrencyFormatter id="formateer" thousandsSeparatorTo="," decimalSeparatorTo="."
                              precision="2" currencySymbol="" rounding="none" />

My output: We dont have any problem upto 16 digits

original-->1234567890123456
Number(txtInput.text)-->1234567890123456
formatted-->1,234,567,890,123,456.00

Erroneous output:

original-->12345678901234567
Number(txtInput.text)-->12345678901234568
formatted-->12,345,678,901,234,568.00 

Here the last digit 7 is rounded to 8.

Erroneous output:

original-->12345678901234567890
Number(txtInput.text)-->12345678901234567000
formatted-->12,345,678,901,234,567,000.00

I have debugged the code and had gone into the format() method CurrencyFormatter . There actually the problem occurs from the Number conversion. I am wondering since the Number.MAX_VALUE is 1.79769313486231e+308 .

Also I found one more weird behavior of the Number. I described below,

var a:Number = 2.03;
var b:Number = 0.03
var c:Number = a- b;

trace("c --> "+c);
Output : c --> 1.9999999999999998

This kind of output is obtaining for this numbers only.

Please suggest me how to solve this issue or suggest me a workaround method.

Thanks in advance. Vengatesh s

Upvotes: -1

Views: 65

Answers (1)

alebianco
alebianco

Reputation: 2555

It's a common problem with big numbers in languages that use 64-bit floating point arithmetic (Actionscript and Javascript are the same in this, to make an example).

It has nothing to do with the CurrencyFormatter, if you try to trace(12345678901234566+1) you'll get 12345678901234568. That's because that number has so many digits that fills the 64-bit storage space and so it gets rounded off. I realise the explanation is quite simplistic, the argument is in fact quite complex.

There are a few BigInt libraries already available (i think as3crypt has one) that can be used if you have to do some arithmetic ... for the formatting i think you'll have to roll your own

EDIT: out of curiosity, you can use this to see how your number is being represented in the IEEE754 binary format

Upvotes: 0

Related Questions