import java.math.BigDecimal;
import java.math.RoundingMode;
public class RemoveZeroes {
static final int SCALE = 10; // decimal range 0.1 ... 0.0000000001
static final String PADDING = "0000000000"; // SCALE number of zeroes
public static void main(String [] arg) {
BigDecimal [] testArray = {
new BigDecimal(0.27),
new BigDecimal(0.1),
new BigDecimal(0.02),
new BigDecimal(0.003),
new BigDecimal(0.0000000001),
};
for (int i = 0; i < testArray.length; i++) {
// normalize to the same scale
BigDecimal b = testArray[i].setScale(SCALE, RoundingMode.FLOOR);
// pad on the left with SCALE number of zeroes
String step1 = PADDING + b.unscaledValue().toString();
// remove extra zeroes from the left
String step2 = step1.substring(step1.length() - SCALE);
// remove extra zeroes from the right
String step3 = step2.replaceAll("0+$", "");
// print result
System.out.println(step3);
}
}
}