In java, how do I get the first nonzero digit of BigDecimal?
For instance:
0.001 => 1 0.02 => 2 987.654 => 9
For numbers from 0 to 1, this will work:
bigDecimal.scaleByPowerOfTen(bigDecimal.precision()).setScale(0, RoundingMode.DOWN)
For numbers greater than 1, this will work:
bigDecimal.scaleByPowerOfTen(1-bigDecimal.precision()).setScale(0, RoundingMode.DOWN)
But is there a solution that works for any number?
source share