dimanche 23 juillet 2017

Java random generate price in two decimal format [duplicate]

This question already has an answer here:

I wanted to random generate price in the range from 0 to 2.10 using Java. I wanted the price in the format like 0.00, 0.10, 0.20, 0.30, 0.40, 0.50 .... 1.10, 1.20 all the way to 2.10. My code as such:

private static DecimalFormat df2 = new DecimalFormat(".##");
double min = 0;
double max = 2.10;
double diff = max - min;
double randomValue = min + Math.random( ) * diff;
System.out.println(df2.format(randomValue));

However, it printed out values like .06, 1.0, .99, .02 etc. Is there anyway to format it?




Aucun commentaire:

Enregistrer un commentaire