Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove leading zero and delimiter char for BigDecimal

Our application can get following numbers:

0.1
0.02
0.003

etc.

These values treated by our code as BigDecimal,as far we operate with money.

There is form on web UI, where user should view these floating parts of prices, transformed to following ones:

1
02
003

The question is,how to trim leading zero and delimiter character in input prices. Perhaps BigDecimal class has standard method something like trimLeadingZeroes(),but can't find any.

UPDATE: trim just leading zero and delimiter symbol

For instance:

1 is 0.1

27 is 0.27
like image 520
sergionni Avatar asked Dec 04 '22 20:12

sergionni


1 Answers

Something like this?

public String getDecimalFractions(BigDecimal value) {
    String strValue = value.toPlainString();
    int index = strValue.indexOf(".");
    if(index != -1) {
        return strValue.substring(index+1, strValue.length());
    } 
    return "0"; 
}
like image 195
Dan MacBean Avatar answered Dec 21 '22 19:12

Dan MacBean