Fast double to string conversion with given precision

前端 未结 4 1128
遥遥无期
遥遥无期 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:30

    If you need both speed and precision, I've developed a fast DoubleFormatUtil class at xmlgraphics-commons: http://xmlgraphics.apache.org/commons/changes.html#version_1.5rc1

    You can see the code there: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/util/DoubleFormatUtil.java?view=markup

    It's faster than both DecimalFormat/BigDecimal, as fast as Double.toString, it's precise, it's well tested. It's licensed under Apache License 2.0, so you can use it as you want.

提交回复
热议问题