Remove the leading 0's till the decimal

前端 未结 3 1514

I want to remove the leading zeros in the decimal numbers.

So i want the output should be .324 not 0.324. I tried str.replaceFi

3条回答
  •  孤街浪徒
    2021-01-17 07:49

    if you are trying to format BigDecimal having value 0.324 to .324. Then below works

        BigDecimal bd=new BigDecimal("0.23");// this will replace with your BigDecimal
        DecimalFormat df=null;
        //check if number is a fraction
        if(bd.remainder(BigDecimal.TEN).compareTo(BigDecimal.ZERO)>0)
          df=new DecimalFormat(".###");
        else
          df=new DecimalFormat("##");
    
        System.out.print(df.format(bd));// .format returns string as .23
    
    0.23->.23
    0->0
    90->90
    80.12->80.12
    

提交回复
热议问题