当前位置:   article > 正文

mysql double 转string类型转换_java中double类型转换成字符串自动格式化成科学计数法...

mysql double java

/**

* Returns the string representation of the double argument.

*

* The representation is exactly the one returned by the

* Double.toString method of one argument.

*

* @param d a double.

* @return a string representation of the double argument.

* @see java.lang.Double#toString(double)

*/

public static String valueOf(double d) {

return Double.toString(d);

}

而Double自身的toString方法使用FloatingDecimal来对数字进行格式化,代码如下:

public static String toString(double d) {

return new FloatingDecimal(d).toJavaFormatString();

}

该方法注释中写到:

*

If m is less than 10 -3 or greater than or

* equal to 107, then it is represented in so-called

* "computerized scientific notation." Let n be the unique

* integer such that 10n <= m <

* 10n+1; then let a be the

* mathematically exact quotient of m and

* 10n so that 1 <= a < 10. The

* magnitude is then represented as the integer part of a,

* as a single decimal digit, followed by '.'

* ('\u002E'), followed by decimal digits

* representing the fractional part of a, followed by the

* letter 'E' ('\u0045'), followed

* by a representation of n as a decimal integer, as

* produced by the method {@link Integer#toString(int)}.

可以很明确的看到,如果数字大于10的7次方或者小于10的-3次方,就会使用科学计数法。这个在很多时候很适用,同时在很多地方也让很多人很头疼。想要避免这个问题,就要自己使用格式化类去重新格式化。最简单的使用DecimalFormat类去格式化,用这个可以很容易的得到不转换成科学计数法的字符串。

DecimalFormat df = new DecimalFormat("###0.0#");//最多保留几位小数,就用几个#,最少位就用0来确定

String s=df.format(d);

这样就可以了~

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2011-11-23 17:27

浏览 15810

评论

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/688325
推荐阅读
相关标签
  

闽ICP备14008679号