我的eclipse工作区中有两个项目,它们有类似的任务.这两个项目都有一个特殊的部分,我将double转换为String.
我通过调用String.valueOf(var)来完成这两个项目.
在较旧的项目中,我总是以“-0.00097656”的格式获得数字,这是我需要的格式.在较新的一个中,我获得了第十个指数格式,如“-9.765625E-4”.我还有一个事实,即旧项目将字符串剪得足够小.
我的问题是:哪些命令可能导致java更改默认转换输出的行为.我已经搜索了代码,但我没有看到这样做的东西.或者它可能是一个日食选项?
我想让新项目与旧项目保持一致,我不希望每次都在新项目中使用这些字符串格式调用.旧项目中的任何地方都必须有设置或一些电话……
希望有人可以暗示一下.
最佳答案:
它在javadoc中有清楚的解释(见Double.toString()) – 格式取决于数字的大小:
If m is greater than or equal to 10-3 but less than 107, then it is represented as the integer part of m, in decimal form with no leading zeroes, followed by ‘.’ (‘\u002E’), followed by one or more decimal digits representing the fractional part of m.
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 Integer.toString(int).
如果您需要特定格式,请使用String.format()或类似内容.
标签:java,format,double,string
来源: https://codeday.me/bug/20190515/1109420.html