• 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

Question about format()

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! I have the following code:


After running this code I get the following output:
123,456001
true
123,456000

On line 1 the float value is passed as an argument to the formatter. As far as my understanding concers the output here should be 123,456000 but the result is 123,456001. On line 3 I pass a double value as an argument and the result is as I expect. How come passing float value is different from passing double value?

The second question concerns line 2. The formatting data expect boolean value but I pass float and the compiler is happy and prints "true". Does it do some type coercion or is there some weird rule about that? Sure, I'm missing something very important but please clear my understanding about this behaviour. Thanks!

 
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

Andris Ratas wrote:
The second question concerns line 2. The formatting data expect boolean value but I pass float and the compiler is happy and prints "true". Does it do some type coercion or is there some weird rule about that? Sure, I'm missing something very important but please clear my understanding about this behaviour. Thanks!



That's how it is defined in the JavaDoc.

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html

Specifically, in the table, in that page.


Conversion ---- Description
'b', 'B' ---------- If the argument arg is null, then the result is "false". If arg is a boolean or Boolean, then the result is the string returned by String.valueOf(). Otherwise, the result is "true".



Henry
 
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

Andris Ratas wrote:
On line 1 the float value is passed as an argument to the formatter. As far as my understanding concers the output here should be 123,456000 but the result is 123,456001. On line 3 I pass a double value as an argument and the result is as I expect. How come passing float value is different from passing double value



That's how floating point works. There is only so much precision. And sometime it is just not accurate.

And BTW, since the system keeps the floating point in base 2, and you are envisioning it in base 10, sometimes, what looks like it should be simple -- in terms of precision, is not.

Henry
 
Andris Ratas
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for quick replay. That clears things up a bit!
 
reply
    Bookmark Topic Watch Topic
  • New Topic