In Javascript it is converted to a call to "toFixed(2)"
Javascript produces the same result - both the dart2js output, and manually coded. So I expect that dart is simply matching javascript behavior in this case.
The documentation on the toStringAsFixed method doesn't actually say that it does any rounding. Just that it returns "fractionDigits" after the decimal.
Moises Belchin wrote:
num numero = 2.275;
print('$numero :: :: ${numero.toStringAsFixed(2)}');
--For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
$ jsc
> numero=2.275
2.275
> print (numero);
2.275
numero=2.275
2.275
console.log(numero)
2.275
0.1+0.2
0.30000000000000004
I'm puzzled by something about this. If run "jsc" and experiment I see the following:
$ jsc
> numero=2.275
2.275
> print (numero);
2.275The same happens in chrome's dev console:
numero=2.275
2.275
console.log(numero)
2.275Why does Javascript not print out the number using all significant digits? I expect to see the full precision of the floating point number and see the erro in its approximation of 2.275.
By way of contrast, the error in the floating point point representation is clear using the common 0.1 + 0.2 example:
0.1+0.2
0.30000000000000004
The results are identical if one plays with Python (a good thing....).
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
On Fri, Jul 19, 2013 at 10:06 PM, Jim Trainor
<jim.train...@gmail.com <mailto:jim.train...@gmail.com>>
okay, I see, makes sense for output to a console. Thanks.
21 July, 2013 5:14 PM
19 July, 2013 5:08 PM
okay, I see, makes sense for output to a console. Thanks.
Florian Loitsch wrote:
19 July, 2013 5:04 PM
19 July, 2013 4:06 PM
I'm puzzled by something about this. If run "jsc" and experiment I see the following:
$ jsc
> numero=2.275
2.275
> print (numero);
2.275The same happens in chrome's dev console:
numero=2.275
2.275
console.log(numero)
2.275Why does Javascript not print out the number using all significant digits? I expect to see the full precision of the floating point number and see the erro in its approximation of 2.275.
By way of contrast, the error in the floating point point representation is clear using the common 0.1 + 0.2 example:
0.1+0.2
0.30000000000000004The results are identical if one plays with Python (a good thing....).
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
19 July, 2013 7:41 AM
On Fri, Jul 19, 2013 at 1:18 PM, jim.trainor.kanata <jim.train...@gmail.com> wrote:
In Javascript it is converted to a call to "toFixed(2)"
Javascript produces the same result - both the dart2js output, and manually coded. So I expect that dart is simply matching javascript behavior in this case.
The documentation on the toStringAsFixed method doesn't actually say that it does any rounding. Just that it returns "fractionDigits" after the decimal.It does round, but it rounds the actual value.Doubles are rationals that are (non-evenly) distributed through the range -10^308 to 10^308. Given that they only have 64bits this means that there are gaps between adjacent doubles (bigger gaps for bigger numbers, smaller gaps for smaller numbers). This also means that many numbers, like 0.1(or 2.275), don't have a precise double equivalent. Instead, you get the closest double. It turns out that for 0.1 the closest double has the value ~0.10000000000000000555. For 2.275 the closest double is ~2.2749999999999999112. You usually don't see these long numbers because the toString() method finds the shortest decimal representation that is still the closest (which by construction is 0.1, resp. 2.275).When you round (or truncate to 2 digits after the decimal point) the rounding algorithm uses the precise number and not the shortest decimal number. This means that it will round 2.2749999999999999112 and not 2.275. This is the reason that you get "2.27"Note that this is not the only problem you encounter when dealing with doubles. Much more common are issues like 0.1 + 0.2 != 0.3. Try it: `print(0.1 + 0.2);`.I recommend reading "What Every Computer Scientist Should Know About Floating-Point Arithmetic" if you want to know more.If you are working with money I strongly recommend using integers instead (just premultiply any incoming value). For example, instead of dealing with Euros: 3.2Eur you could deal with cents: 320. If that's not enough: 3200 tenth-of-a-cent. Ideally you should wrap this away into a Currency (or similar class). There are also packages (like http://pub.dartlang.org/packages/decimal) but I can't comment on them (never used them).
Moises Belchin wrote:
num numero = 2.275;
print('$numero :: :: ${numero.toStringAsFixed(2)}');
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
--
Give a man a fire and he's warm for the whole day,
but set fire to him and he's warm for the rest of his life. - Terry Pratchett