Built in methods for displaying Significant figures

前端 未结 2 790
梦谈多话
梦谈多话 2021-01-19 16:48

There\'s been a lot of questions on rounding by significant figures, and answers that that provides a new method to do the rounding, such as:

Rounding to an arbitrar

2条回答
  •  梦谈多话
    2021-01-19 17:39

    Not quite right. To get it perfect:

    public String toSignificantFiguresString(BigDecimal bd, int significantFigures ){
        String test = String.format("%."+significantFigures+"G", bd);
        if (test.contains("E+")){
            test = String.format(Locale.US, "%.0f", Double.valueOf(String.format("%."+significantFigures+"G", bd)));
        }
        return test;
    }
    

提交回复
热议问题