Java实现特殊小数格式化

需求是:小数最多展示2位,如果没有就不展示。

        NumberFormat nf = NumberFormat.getNumberInstance();
        // max 2 digits
        nf.setMaximumFractionDigits(2);
        // min 0 digits
        nf.setMinimumFractionDigits(0);
        nf.setRoundingMode(RoundingMode.HALF_UP);
        nf.setGroupingUsed(false);
        return nf.format(double_number);

 

Leave a Reply

Your email address will not be published. Required fields are marked *