This week's book giveaway is in the JDBC and Relational Databases forum.
We're giving away four copies of Murach's MySQL and have Joel Murach on-line!
See this thread for details.
Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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

String to double losing decimal places

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does the following occur? A double can hold these numbers can't it?

source = "1234567890123.1234";
dvalue = Double.parseDouble(source);
result = formatter.format(dvalue);
out.print(source + " = " + result + br);
//gives1234567890123.1234 = 1,234,567,890,123.1233

source = "12345678901234.1234";
dvalue = Double.parseDouble(source);
result = formatter.format(dvalue);
out.print(source + " = " + result + br);
//gives12345678901234.1234 = 12,345,678,901,234.123

source = "123456789012345.1234";
dvalue = Double.parseDouble(source);
result = formatter.format(dvalue);
out.print(source + " = " + result + br);
//gives123456789012345.1234 = 123,456,789,012,345.12
 
author
Posts: 23949
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
As you probably figured out by now, floating point numbers have restrictions in both range and precision.

If you exceed the range, Java will complain. If you exceed the precision, it will just round it off.

Henry
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And if you need more precision, you should take a look at the BigDecimal class.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic