# Thread: number of floating point

1. Member
Join Date
Dec 2008
Posts
4
Rep Power
0

## number of floating point

hi. i wrote a program that makes float numbers between 0 and 1 .number of floating point is 8 foe example 0.01234444.but i want to make this randoms with 2 flaoting points for example 0.22.but i cant.please help me.:confused:

2. Senior Member
Join Date
Nov 2008
Posts
286
Rep Power
6
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;`
- if you need to calculate with such numbers, and the precision is completely paramount, then look at the BigDecimal class (which you can instantiate with two random digits).

In the examples above, 'rand' represents an instance of Random, of course.

3. 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.

4. Java 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();
}
}
}```

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));```
The "#,###.00" shows the number of places to show after the decimal. You can add or subtract zeros and number signs to change your look. Of course, this is not exact, but you can plug these lines into your code and it should work fine :)

Cheers

6. Member
Join Date
Dec 2008
Posts
4
Rep Power
0
Thanks all of you very much.i have a project with GA and you solved my problem.Thank you very much again.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•