I want manage numbers on a range from:
from 0,001
to 999,999
For representation reasons, I want to drop some of the accuracy keeping only the 3 most important digits of the number.
For the number 123,12
I expect the result 123
.
For the number 12,123
I expect the result 12,1
.
For the number 0,001
I expect the result 0,001
.
The best solution I thought of is transforming the number into a String, and back again to double, this way:
number = number*1000;
String s = new String(number);
s = s.substr(0, 3) + "000";
number = Double.parseDouble(s);
number = number/1000;
This does the job but it looks both poorly performing and not elegant. Any more clever alternative? Thank you!