• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NuberFormat format and parse doubt

 
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
reply
    Bookmark Topic Watch Topic
  • New Topic