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:

Printable View

- 12-28-2008, 01:52 AMmohammad8065number 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:

- 12-28-2008, 03:33 AMneilcoffey
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*displa*y 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:

Code:`double randDecimal = (double) rand.nextInt(100) / 100d;`

In the examples above, 'rand' represents an instance of Random, of course. - 12-28-2008, 03:35 AMFubarable
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. - 12-28-2008, 03:47 AMhardwiredCode:
`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();

}

}

}

- 12-28-2008, 04:45 AMKingZebulon
I had this problem in the past, and the DecimalFormat class worked the best for me.

Code:`import java.text.DecimalFormat;`

float floatNum;

DecimalFormat df = new DecimalFormat("#,###.00");

System.out.println(df.format(floatNum));

Cheers - 12-28-2008, 10:41 AMmohammad8065
Thanks all of you very much.i have a project with GA and you solved my problem.Thank you very much again.