Java random digit to round up the seond decimal place | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Java random digit to round up the seond decimal place

I using java Math.random to generate any double digit For example: 13.56 how to make this>>>>13.60 13.54 how to make this>>>>15.50 How to make sure i round up to second decimal place to be zero

18th Feb 2022, 3:32 AM
Jacky
1 Answer
+ 1
- start with a double of any precision - multiply by 10 - use Math.round() - divide by 10.0 Result is a double with one decimal digit. Make sure to divide by 10.0 (double) and not by 10 (int) because Math.round will return a long type, and you want to do floating point division rather than integer division.
18th Feb 2022, 5:10 AM
Tibor Santa
Tibor Santa - avatar