gpt4 book ai didi

Java 默认数字到字符串的行为

转载 作者:搜寻专家 更新时间:2023-10-31 19:49:44 25 4
gpt4 key购买 nike

我的 eclipse 工作区中有两个项目,它们的任务相似。这两个项目都有一个特殊的部分,我将 double 转换为 String

我的两个项目都是通过调用 String.valueOf(var) 来完成的。

在较旧的项目中,我总是以“-0.00097656”之类的格式获取数字,这是我需要的格式。在较新的版本中,我得到了十分之一的指数格式,例如“-9.765625E-4”。我还有一个事实,即旧项目将字符串切得足够小。

我的问题是:哪些命令会导致 java 更改默认转换输出的行为。我已经搜索了代码,但我没有看到这样做的东西。或者它可能是一个 eclipse 选项?

我想让新项目与旧项目保持一致,我不想在新项目中每次都使用这些字符串格式调用。旧项目中的任何地方都必须有一个设置或一些调用......

希望有人能给点提示。

最佳答案

它在 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 默认数字到字符串的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5337432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com