As mentioned above. I give one example, let's say all the test values are less than 1 but greater than 0.
how do i convert those value without hard-coding the scale and precision value.
for 0.1 and 0.01 and 0.001 should get 1 (i know this i bad idea but i have been given sets of business rules of the software)
i prefer solution in java but if there is a math algorithm it is better. thanks.
The solution is much simpler then anybody presented here. You should use BigDecimal:
BigDecimal a = new BigDecimal("0.0000012");
BigDecimal b = a.movePointRight(a.scale());
Multiply by 10 until trunc x = x.
Something like (untested code):
double val = ... ; // set the value.
while(Math.floor(val) != val)
val *= 10.0;
One option would be to simply convert to string, split on the decimal point, and grab the portion after it back into an integer:
Integer.parseInt((Double.toString(input_val).split('\\.'))[1])
(Note: you may want to do some error checking as part of the process; the above example code is a condensed version just designed to convey the point.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With