Results 1 to 4 of 4
Thread: Floats and Doubles and Currency
 03082014, 01:04 PM #1Member
 Join Date
 Feb 2014
 Posts
 20
 Rep Power
 0
Floats and Doubles and Currency
Hi guys,
Just done a quick test to try and figure out the difference between floats and doubles.
I made a quick program which outputs the result of 3.3 * 2 as both a float and a double.
I assumed since a float is the larger and more precise of the two data types that there would be more numbers after the decimal point, however this was not the case, it was in fact the double which had more numbers after the decimal point. Result was as follows:
Float: 29.699999
Double: 29.7
BTW for the above code I simply had a few text fields and a button with the following code:
Java Code:textBox3.setText("" + Float.parseFloat(textBox1.getText()) * Float.parseFloat(textBox2.getText())); textBox6.setText("" + Double.parseDouble(textBox1.getText()) * Double.parseDouble(textBox2.getText()));
Also, as these are limited to a certain amount of numbers im thinking there must be a more precise way for currency, if so what would I use for that?
I suppose what im trying to figure out is what data type to use in different scenarios? When to use integer, float, double and long.
Thanks in advance for any help I may get.
 03082014, 02:09 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 29
Re: Floats and Doubles and Currency
A float uses 32 bits for its mantissa and exponent, while a double uses 64 bits; a double can store more decimals. Be very careful if floats or doubles have to represent money; for one thing, both can't exactly represent the simple fraction 1/10 (0.1). Better rescale your monetary units and do all your arithmetic in cents and use simple ints or longs. This also fails if you want to use fractions of cents. You have to use BigDecimals then ... Also read this: What Every Computer Scientist Should Know About FloatingPoint Arithmetic, it's a must read if you want to do some serious floating point stuff.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
 03082014, 06:37 PM #3Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 15
Re: Floats and Doubles and Currency
In fact, the only fractions that can be accurately represented in floating point are those whose denominators are only powers of two. This is analogous to decimal where the terminating (i.e. nonrepeating) fraction's denominator must consist only of powers of two and five (the two prime factors if 10).
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 03092014, 01:56 PM #4Member
 Join Date
 Feb 2014
 Posts
 20
 Rep Power
 0
Similar Threads

Trouble with floats and doubles
By Humphrey Bogart in forum New To JavaReplies: 2Last Post: 04012013, 07:23 PM 
Cost of using BigDecimal vs Floats?
By SnakeDoc in forum New To JavaReplies: 3Last Post: 08192012, 10:05 AM 
JFormattedTextField and Currency
By Dcalladi in forum AWT / SwingReplies: 1Last Post: 02192012, 10:00 PM 
java currency
By alfarida in forum New To JavaReplies: 4Last Post: 05232011, 02:11 PM 
Setting currency
By Java Tip in forum Java TipReplies: 0Last Post: 11162007, 03:08 PM
Bookmarks