Results 1 to 5 of 5
Thread: tiny offset in calculation
- 07-22-2011, 04:58 AM #1
Member
- Join Date
- Jul 2011
- Posts
- 2
- Rep Power
- 0
- 07-22-2011, 05:58 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You can use formatting to specify how many decimal places. That's the only fix I can think of. Using floating point numbers in computers is simply not exact. There are a lot of resources on this topic around the internet if you are willing to read to understand it. Otherwise, you can just understand that using floating point will not produce exact results, and you should be careful if calculations require exact results.
To format with String.format or System.out.printf(), use the %f flag, look up how to use them, and specify how many decimal places to show.
- 07-22-2011, 09:31 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-22-2011, 04:26 PM #4
Member
- Join Date
- Jul 2011
- Posts
- 2
- Rep Power
- 0
Thanks
Thanks for the help :)
- 07-22-2011, 05:50 PM #5
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
There are a couple of approaches to this:
(1) not care about the inaccuracy in the last decimal place and treat it as a display problem. As mentioned, you can do something like System.out.println("%.2f", number); to display to (in this case) 2 decimal places; (See the NumberFormat class if you need more control)
(2) if you actually care about the inaccuracy for some reason, then see the BigDecimal class.Neil Coffey
Javamex - Java tutorials and performance info
Similar Threads
-
wunderground.com API GMT offset
By SteveG in forum New To JavaReplies: 2Last Post: 07-13-2011, 08:03 PM -
Tiny problem with Inheritance
By MaceMan in forum New To JavaReplies: 16Last Post: 04-19-2011, 02:14 PM -
Tiny Problem with JFrame
By mr_anderson in forum AWT / SwingReplies: 3Last Post: 05-27-2010, 08:50 PM -
'Tiny' 3D Problem
By Supamagier in forum Advanced JavaReplies: 2Last Post: 09-03-2009, 09:34 AM -
Tiny Eclipse 3.3
By JavaBean in forum Java SoftwareReplies: 0Last Post: 07-09-2007, 04:34 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks