trimBigDecimal ( (BigDecimal) a) to cut off all trailing zeros. It's a terrible hack. @param n the BigDecimal to trim all trailing zeros from @return the trimmed BigDecimal
(BigDecimal n)
| 37 | * @return the trimmed BigDecimal |
| 38 | */ |
| 39 | public static BigDecimal trimBigDecimal(BigDecimal n) |
| 40 | { |
| 41 | if (n.unscaledValue().intValue() == 0) |
| 42 | { |
| 43 | // Java 1.5 will not throw an ArthmeticException if you change the |
| 44 | // scale of 0.0 to 0, so it will keep going through the loop below |
| 45 | // forever. To get around this we test for the special case here. |
| 46 | return BigDecimal.ZERO; |
| 47 | } |
| 48 | |
| 49 | if (n.scale() <= 0) |
| 50 | { |
| 51 | return n; |
| 52 | } |
| 53 | |
| 54 | BigDecimal stripped = n.stripTrailingZeros(); |
| 55 | if (stripped.scale() < 0) |
| 56 | { |
| 57 | stripped = n.setScale(0); |
| 58 | } |
| 59 | |
| 60 | return stripped; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Returns a string with the trimmed number. |
no test coverage detected