1

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?

Am_I_Helpful
  • 17,636
  • 7
  • 44
  • 68
hyperfkcb
  • 1,987
  • 6
  • 29
  • 64
  • Nope does not work. I still see some values like .03, .07 etc – hyperfkcb Jul 24 '17 at 03:26
  • If you want all your random values to be multiples of 0.10, then generate a random integer and multiply it by 0.10 (or divide by 10). Look at the `Random` class for ways to generate random integers. – ajb Jul 24 '17 at 03:26

4 Answers4

2

You can make it generate numbers ending in 0 like this:

public static void main(String[] args){
    DecimalFormat df2 = new DecimalFormat("0.0");
    double min = 0.00;
    double max = 2.10;
    double rand = new Random().nextDouble();
    String result = df2.format(min + (rand * (max - min))) + "0";
    System.out.println(result);
}

It just rounds the decimal to the tenth place, then adds 0 to the output, generating the numbers you want.

Here are some examples of the output:

1.90
1.50
0.70
2.10
1.40
1.10
0.10
0.00
0.60
  • I get `.20`.... – John Joe Jul 24 '17 at 03:33
  • That's what the OP wants. –  Jul 24 '17 at 03:33
  • _"I wanted the price in the format like 0.00, 0.10, 0.20, 0.30, 0.40, 0.50 ...."_ – John Joe Jul 24 '17 at 03:34
  • 1
    @JohnJoe I edited my post. –  Jul 24 '17 at 03:34
  • @CodingNinja Hey there is some problem by adding the "0" at the back. I get some random value like 20, 30 etc – hyperfkcb Jul 24 '17 at 03:37
  • That's because `"#.#"` causes zeroes not to show up. So if the value (rounded to the nearest tenth) is 2, the result of `format` is `"2"`, not `"2.0"`. That's why you see things like `"20"`. Try changing the format to `"#.0"`, or maybe better, `"0.0"`. See http://docs.oracle.com/javase/8/docs/api/java/text/DecimalFormat.html. – ajb Jul 24 '17 at 03:44
  • Another problem about this solution is that you can't get the price 2.10 since Math.random() is [0, 1), you won't get 1 for the function. – Elvin Jul 24 '17 at 03:47
  • @DeniseTan I have edited my post and now it works. You won't get those values anymore. Thanks for your patience! –  Jul 24 '17 at 04:57
  • @CodingNinja Thanks so much! It works! – hyperfkcb Jul 24 '17 at 06:48
2

As I stated in the comments, this is very easily possible by multiplying randomValue result by 10, then applying Math.floor() on it, and then finally dividing the result obtained in the previous step would do it for you.

NOTE: (In the comments section, my fingers typed an extra 0 at the end! I have removed the comment though)

Working code would be:

// use the correct pattern #0.00
DecimalFormat formatter = new DecimalFormat("#0.00");  // edited here.    
double randomValue = min + Math.random( ) * diff;
double tempRes = Math.floor(randomValue * 10);
double finalRes = tempRes/10;
System.out.println(formatter.format(finalRes));
Am_I_Helpful
  • 17,636
  • 7
  • 44
  • 68
2

I have another straightforward solution and maybe it's the simplest one? think about it in this way: 0.00, 0.10, 0.20 ... 2.10, actually you only need to generate random integers from 0 (inclusive) to 22 (exclusive) and then divide it by 10. Then you can use the result to format.

For example, the random number you have is 18, then the result is 1.8, then you can format it as 1.80;

Random rand = new Random();
System.out.printf("%.2f%n", rand.nextInt(22) / 10.0);

Same logic, if you want to have price with minimum gap of 0.01, you could generate random number from 0 (inclusive) to 211 (exclusive) and divide it by 100;

No other operation needed for handling 0s.

Elvin
  • 937
  • 5
  • 15
  • Is there anyway not to use printf in this case? Because I will be formating the output using System.out.println – hyperfkcb Jul 24 '17 at 03:45
  • @DeniseTan May I know is there any reason to do so? because the %n in printf is for new line as ln in println – Elvin Jul 24 '17 at 03:48
  • You can use `String.format`, which uses the same type of format string as `printf` but doesn't print anything. See [javadoc](http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#format-java.lang.String-java.lang.Object...-) – ajb Jul 24 '17 at 03:49
  • Yes, as @ajb said, you can format the string using String.format, and use it with System.out.println – Elvin Jul 24 '17 at 03:50
1

Sorry for not read it carefully.

private static DecimalFormat df2 = new DecimalFormat("#.#");
...
System.out.println(df2.format(randomValue) + "0");
Manh Le
  • 1,572
  • 15
  • 26