Results 1 to 6 of 6
Thread: number of floating point
 12282008, 01:52 AM #1Member
 Join Date
 Dec 2008
 Posts
 4
 Rep Power
 0
 12282008, 03:33 AM #2Senior Member
 Join Date
 Nov 2008
 Posts
 286
 Rep Power
 8
OK, it depends exactly on what guarantee you need about the number and what use you want to put to it.
Remember that when you create a floating point number, it is generally not completely accurate. In particular, if the fraction is not a negative power of two (1/2, 1/4, 1/8 etc) or a combination of such fractions, then the number cannot be stored completely accurately. So there is no way to guarantee that a 'float' or 'double' will be accurate to precisely two and only two decimal places. If you "ask" for 0.22 etc, you will actually get something like 0.2200000000001 etc. So depending on what you need, you have a few options:
 if you just need to display two random digits but "pretend" that they represent a fraction between 0 and 1, then you may as well just generate two random decimal digits with rand.nextInt(10) and manually print a decimal point followed by the digits (you can also do rand.nextInt(100), remembering to adjust your display code for the case of '00');
 if you need to choose or calculate with a random choice from the values 0.01, 0.02, 0.03 etc, but the inaccuracy of float/double doesn't bother you, then you can use:
Java Code:double randDecimal = (double) rand.nextInt(100) / 100d;
In the examples above, 'rand' represents an instance of Random, of course.Neil Coffey
Javamex  Java tutorials and performance info

There are two issues here, one being the significant digits of the number itself, which I think you don't want to mess with, and the other being the significant digits of the number as it is displayed, and this you probably want to control.
So on that note, I recommend that you look into either String.format(...) or DecimalFormat to control how your floating point number is displayed.
 12282008, 03:47 AM #4Java Code:
import java.util.Random; public class Test { public static void main(String[] args) { Random r = new Random(); for(int i = 0; i < 10; i++) { double f = r.nextFloat(); System.out.println("f = " + f); System.out.printf("\tformatted = %.2f", f); float truncated = ((int)(f*100))/100f; System.out.print(" truncated = " + truncated); float rounded = Math.round(f*100)/100f; System.out.print(" rounded = " + rounded); System.out.println(); } } }
 12282008, 04:45 AM #5
I had this problem in the past, and the DecimalFormat class worked the best for me.
Java Code:import java.text.DecimalFormat; float floatNum; DecimalFormat df = new DecimalFormat("#,###.00"); System.out.println(df.format(floatNum));
Cheers
 12282008, 10:41 AM #6Member
 Join Date
 Dec 2008
 Posts
 4
 Rep Power
 0
Similar Threads

Floating point values in SWT Spinner
By Java Tip in forum SWT TipsReplies: 0Last Post: 07072008, 04:50 PM 
is it bug? or am i missing a point?
By blowguy in forum Threads and SynchronizationReplies: 1Last Post: 11202007, 07:19 PM
Bookmarks