Monday, April 22, 2013

Re: NumberFormat output wrong?

On 16/04/2013 18:43, Thad wrote:
> NumberFormat has not worked the way I expected
> (see https://groups.google.com/d/msg/google-web-toolkit/zlbRIhTKqrA/cyjaDB12K6UJ for an
> experience with devmode vs production).
>
> On Monday, April 15, 2013 3:29:30 PM UTC-4, Patrick Tucker wrote:
>
> Today I noticed that the output of my NumberFormat (GWT 2.5.0) is not what I thought
> it had been set to. So I played with it a little and found the output to be inconsistent.
> The formats:
>
> 1: NumberFormat.getFormat("#,###.##")
> 2: NumberFormat.getFormat("#,###.00")
> 3: NumberFormat.getFormat("#,##0.00")
>
> The output is as follows:
>
> 1: 3.57, 0.98, 0.6
> 2: 3.57, .98, .60
> 3: 3.57, 0.98, 0.60
>
> The NumberFormat class doc states the following:
>
> 0 Number Yes Digit
> # Number Yes Digit, zero shows as absent
>
> According to the doc comments, show above, I was expect the following output for each
> format:
>
> 1: 3.57, .98, .6
> 2: 3.57, .98, .60
> 3: 3.57, 0.98, 0.60
>
> What going on here? The output for 1 should not have the leading 0, just like the
> 2nd format.

The "zero shows as absent" indication is terse and vague... We can suppose it is about
"non-significant zeroes", but it is better if explicitly stated!

Just note that Java's DecimalFormat just acts the same:

DecimalFormat[] nfs =
{
new DecimalFormat("#,###.##"),
new DecimalFormat("#,###.00"),
new DecimalFormat("#,##0.00")
};
double[] vals = { 3.57, 0.98, 0.6 };
for (DecimalFormat nf : nfs)
for (double v : vals)
System.out.println(nf.format(v));

==>

3,57
0,98
0,6
3,57
,98
,60
3,57
0,98
0,60

(French locale!)
I am not sure how to interpret that when reading the JavaDoc...

--
Philippe Lhoste
-- (near) Paris -- France
-- http://Phi.Lho.free.fr
-- -- -- -- -- -- -- -- -- -- -- -- -- --

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment