BigDecimal to string

后端 未结 8 1659
感情败类
感情败类 2020-12-13 08:06

I have a BigDecimal object and i want to convert it to string. The problem is that my value got fraction and i get a huge number (in length) and i only need the original num

相关标签:
8条回答
  • 2020-12-13 08:56

    For better support different locales use this way:

    DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(2);
    df.setMinimumFractionDigits(0);
    df.setGroupingUsed(false);
    
    df.format(bigDecimal);
    

    also you can customize it:

    DecimalFormat df = new DecimalFormat("###,###,###");
    df.format(bigDecimal);
    
    0 讨论(0)
  • 2020-12-13 08:56

    To archive the necessary result with double constructor you need to round the BigDecimal before convert it to String e.g.

    new java.math.BigDecimal(10.0001).round(new java.math.MathContext(6, java.math.RoundingMode.HALF_UP)).toString()

    will print the "10.0001"

    0 讨论(0)
提交回复
热议问题