Fast double to string conversion with given precision

前端 未结 4 1119
遥遥无期
遥遥无期 2021-02-02 17:14

I need to convert double to string with given precision. String.format(\"%.3f\", value) (or DecimalFormat) does the job but benchmarks show that it slow even in com

4条回答
  •  感情败类
    2021-02-02 17:34

    I haven't benchmarked this, but how about using BigDecimal?

    BigDecimal bd = new BigDecimal(value).setScale(3, RoundingMode.HALF_UP);
    return bd.toString();
    

提交回复
热议问题