简体   繁体   中英

what decimal separator is used by Double.tostring()?

Double.tostring()使用当前本地的小数分隔符还是为所有语言环境和实现修复的东西(即:dot“。”)?

The Double.toString() method code :

public String toString() {
    return toString(value);
}

invokes this static method :

public static String toString(double d) {
    return FloatingDecimal.toJavaFormatString(d);
}

The javadoc of it is refers to the use cases of the method and all refers to . character as separator between the integer and the decimal part :

  • If m is zero, it is represented by the characters "0.0"; thus, negative zero produces the result "-0.0" and positive zero produces the result "0.0" .

  • 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).

It doesn't say explicitly : the separator is . but all use cases of the toString() method of Double explicit the . character as separator.
By deduction, we may so consider . as the character separator.

It always uses . as a decimal separator.

See the java documentation : java文档的屏幕截图

When you study the javadoc for Double.toString() you will find that it mentions the static method Double.toString(double) :

If m is zero, it is represented by the characters "0.0"; thus, negative zero produces the result "-0.0" and positive zero produces the result "0.0".

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 '.' ('\.'), 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 '.' ('\.'), followed by decimal digits representing the fractional part of a, followed by the letter 'E' ('\E'), followed by a representation of n as a decimal integer, as produced by the method Integer.toString(int).

And the javadoc for that method lists in great detail how the result of that call is build up. And it clearly expresses that you need to use a NumberFormat yourself if you want localized output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM