I am facing the problem on multiplying two decimal number in flex.
When i am multiplying two decimal number the result is like a exponential number so i don't know how to get the decimal number as a result instead of getting an exponential number.
I am using the following codes to make a multiplication:
var num1:Number = 0.00005;
var num2:Number = 0.000007;
var result:Number = num1 * num2;
And in result variable i am getting the value as 3.5000000000000003E-10.
So i don't know how to get decimal number as result instead of getting an exponential number as above.
If anybody have knowledge how to resolve this problem please help me to solve.
You need to use the .toPrecision(precision:uint) method available in the Number class. This method takes one parameter which is :
precision:uint — An integer between 1 and 21, inclusive, that
represents the desired number of digits to represent in the resulting
string.
So simply do :
trace(result.toPrecision(2));
And you should get an output of 0.00000000035
Official documentation here :
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Number.html#toPrecision()
Hope this helps. Cheers.
Related
Hi after calling this code (Octave) I get an answer with 7 digits of precision, I need only 6. It is worth mentioning that on different data-set the output is normal(with 6 digits);
output_precision(6);
Prev
output:
Prev =
0.1855318
0.2181108
0.1796457
I know this is a little late but I wanted to add an answer for anyone with the same question in the future.
According to the function reference for output_precision(), the argument passed to the function (in this case, 6) specifies the minimum number of significant figures, which only guarantees that future numeric output won't have less than that number of significant figures.
From what I've seen, if you use output_precision(new_val) before displaying an array (e.g., Prev in the question), then octave will round the element with the least digits before the decimal place to have new_val significant figures and then all other elements will be rounded to have the same number of digits after the decimal place as that initial rounded result. If you use a statement to output a single value instead of an array, then the output is just rounded to new_val significant figures. However, I don't know if this behavior is guaranteed .
Here's a short example of what I mean:
% array defined with values having 5 digits after decimal
F = [401.51670 313.70753 -88.55225 188.50067 280.21988 354.51821 54.51821 350];
output_precision(4)
F
output_precision(6)
F
Output:
F =
401.52 313.71 -88.55 188.50 280.22 354.52 54.52 350.00
F =
401.5167 313.7075 -88.5523 188.5007 280.2199 354.5182 54.5182 350.0000
It can be a little quirky if you try to round the values too much. When I used output_precision(3) and then output F, the numbers were actually rounded as if my system's default precision, 5, was still active. However, when I used elements with only 2 or 3 digits after the decimal to define another array, it displayed as expected with output_precision(3).
Check out Octave Forge if you ever need docs for octave features. It's not perfect but it's something. Hope this was helpful.
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
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
Hello I was wondering how to set the number of bits for a number in java. Eg( integer 2, but i want it to be a 8 bit binary number so 00000010). I have used Integer.toBinaryString but that only prints it out as 10. Thanks in advance
You can use System.out.format() instead of using System.out.println() for this purpose. Here you can specify your desired format, in which you want to print your Integer or Float or any other data type. Try the following code:
int x = 2;
String binary = Integer.toBinaryString(x);
System.out.format("%08d",Integer.parseInt(binary));
In above code, mark the last line. We want to print Integer, so we used %d and we wanted 8 0's before that Integer x so we used 08. Thus the format becomes: %08d. See this for more information about formatting.
Prevously I had a result from a Database Connection SQL statement that resembled the following:
[{"BALANCE":111.11},{"BALANCE":222.12},{"BALANCE":444.30}]
And I used the following contents of an expression node to calculate the sum:
sum = 0;
foreach (row : message.payload) {
sum += row['BALANCE'];
}
message.payload = sum;
This did not quite work out, but notice below that there are no quotes around the numeric variable that was returned
777.5299999999999994315658113919199
From an excellent answer from a previous thread, I switched to the following expression node contents:
sum = 0;
foreach (row : message.payload) {
sum += row['BALANCE'];
}
message.payload = new java.text.DecimalFormat("#.##").format(sum);
This resulted the accurate result below:
"777.53"
My only problem is that it has quotes around the number. How can I eliminate the quotes?
Thanks
The real fix to your problem is the following: do not use floating point numbers to store currency amounts. This is prone to rounding errors (as you've experienced above) and it turns out people don't like to lose cents here and there.
Read https://stackoverflow.com/a/3730040/387927 for more on this.
So fix the database to store only integers, for example in cents, 111111 instead of 111.11, and only perform integer calculations on these cents.
Or review the DB query to return java.math.BigDecimal instances instead of the floats or doubles it seems to be returning. That way you would have no adverse rounding playing tricks on you.
Any other approach where you first would go through a lossy floating point data type and then perform rounding (like in user1760178's answer), will expose you to rounding issues.
You may try converting the sum to BigDecimal and then set scale on it to 2.
message.payload = new BigDecimal(sum).setScale(2,java.math.RoundingMode.CEILING)
This keeps your payload as number instead of a String.
Hope this helps.
I'm performing a calulation that results in a floating point number. When I try and write it to a Access field, I get this error:
Run-time error '3759': Scaling of decimal value resulted in data truncation
I've intentionally limited the access field to a fixed precision. I was hoping that the value would be automatically truncated, but instead it's throwing this error - how can I explicitly change the precision of the value in VBA to avoid this error?
Code:
value = X / Y
With myTAble
.AddNew
!calc = value
.Update
End With
You could try using VBA's Round function, as in:
!calc = Round(value, 2)
Replace the 2 with however many decimal places you want.
Depending on the scale and precision of your numbers, you might still encounter the 3759 error if Round returns a value that cannot be expressed exactly as a Double floating point number (and so ends up having more decimal places than you asked for). A more robust approach might be to use something like:
!calc = CDec(FormatNumber(value, 2))
Replace the 2 with however many decimal places you want.
FormatNumber will round the number and convert it to a string (with only the number of decimal places that you specify), and CDec will convert the string into a Decimal.
How about this:
value = X / Y
With myTAble
.AddNew
!calc = Fix(value*10^decimalPlaces)/10^decimalPlaces
.Update
End With
Where decimalPlaces is the number of decimal places that you wish to work with.
Fix truncates the number. If you wish some kind of rounding, try Clng.