Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NuberFormat format and parse doubt

 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

This question is from the book K&B Java6:



The output is:
987.12346
987.123456

I have two doubts:
1. Why it printed 6 instead of 5 in the first line?
2. If I parse with my default locale, the dot is not printed, but in the US it is. Why US prints dot when parsing?
 
Henry Wong
author
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leandro Coutinho wrote:
I have two doubts:
1. Why it printed 6 instead of 5 in the first line?
2. If I parse with my default locale, the dot is not printed, but in the US it is. Why US prints dot when parsing?


1. Take a look at the RoundingMode of the NumberFormat class. If you want it to just truncate, you'll need to set the mode correctly.

2. Well, in the U.S., the fractional portion of a number is separated by a period. This may not be true everywhere.

Henry
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply!

Henry Wong wrote:
2. Well, in the U.S., the fractional portion of a number is separated by a period. This may not be true everywhere.

Ok, but the parse result should be equal to the format, right? The parse documentation says: "Parses text from the beginning of the given string to produce a number. ...", but the result is a wrong number.

Try this code:

The output is:
987,12346
987123456
 
Henry Wong
author
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leandro Coutinho wrote:
Ok, but the parse result should be equal to the format, right? The parse documentation says: "Parses text from the beginning of the given string to produce a number. ...", but the result is a wrong number.


Yes, the parse() method should match the format() method. And in this case, it doesn't. You configured it for a region that uses a comma, and then you parsed it with a string that uses a period. Just as the comma is ignored in the US format, the period is ignored in the Brazil format.

As for the double side, that always uses a period. NumberFormat is for the string side only.

Henry
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Leandro Coutinho wrote:
Ok, but the parse result should be equal to the format, right? The parse documentation says: "Parses text from the beginning of the given string to produce a number. ...", but the result is a wrong number.


Yes, the parse() method should match the format() method. And in this case, it doesn't. You configured it for a region that uses a comma, and then you parsed it with a string that uses a period. Just as the comma is ignored in the US format, the period is ignored in the Brazil format.

As for the double side, that always uses a period. NumberFormat is for the string side only.

Henry

of course!!

thank you!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic