使用内联条件语句格式化小数

发布于 2024-12-08 16:41:20 字数 282 浏览 1 评论 0原文

我想知道为什么下面的代码不一致。我期望相同的输出,但是当使用内联条件语句时,它会在字符串中附加 .0。 我的代码有错误吗?

    double d = 10.1;

    String rounded = (false ? d : Math.round(d)) + "";
    System.out.println(rounded);//10.0

    rounded = Math.round(d) + "";
    System.out.println(rounded);//10

I'm wondering why there is an inconsistency with the code below. I would expect the same output, but when using the inline conditional statement, it appends a .0 to the string.
Do I have some error in my code?

    double d = 10.1;

    String rounded = (false ? d : Math.round(d)) + "";
    System.out.println(rounded);//10.0

    rounded = Math.round(d) + "";
    System.out.println(rounded);//10

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狠疯拽 2024-12-15 16:41:20

Math.round 返回一个 long,因此条件运算符的两个操作数不具有相同的类型,因此需要遵循更复杂的规则来确定其类型整体操作,如 JLS 中定义§15.25

否则,二进制数字提升(第 5.6.2 节)将应用于操作数
类型,条件表达式的类型为提升类型
第二个和第三个操作数。请注意,二进制数字提升
执行拆箱转换(§5.1.8)和值集转换
(§5.1.13)。

从5.6.2开始,二进制数字提升:

如果其中一个操作数为 double 类型,则另一个操作数将转换为 double。


为了说明条件运算符的陷阱和一些乐趣,来自 Java 谜题(谜题 8):

char x = 'X';
int i = 0;
System.out.print(true ? x : 0); // prints X
System.out.print(false ? i : x); // prints 88 => (int)X 

此外,观看哈姆雷特Elvis 示例(视频链接)。

Math.round returns a long, therefore the two operands of the conditional operator do not have the same type, and thus a more complex rule is followed to determine the type of the overall operation, as defined in JLS §15.25:

Otherwise, binary numeric promotion (§5.6.2) is applied to the operand
types, and the type of the conditional expression is the promoted type
of the second and third operands. Note that binary numeric promotion
performs unboxing conversion (§5.1.8) and value set conversion
(§5.1.13).

And from 5.6.2, binary numeric promotion:

If either operand is of type double, the other is converted to double.


And to illustrate the pitfalls with the conditional operator and for some fun, from Java puzzlers (puzzle 8):

char x = 'X';
int i = 0;
System.out.print(true ? x : 0); // prints X
System.out.print(false ? i : x); // prints 88 => (int)X 

Also, check out the Hamlet and Elvis examples (video links).

拒绝两难 2024-12-15 16:41:20

从三元运算符返回的类型可以被提升,以便两个潜在的返回类型匹配。这称为二进制数字提升,变量在转换为字符串之前从 long 提升为 double。

如果您遇到两个潜在返回类型均为 int 或 long 的情况:

double d = 10.1;      
String rounded = (false ? 0 : Math.round(d)) + ""; 

会发生什么(不是反问句,因为我离 Java 编译器很远)?

The type returned from a ternary operator may be promoted so that the two potential return types match. This is called binary numeric promotion, and your variable is being promoted from long to double before conversion to a String.

If you had this where both potential return types are int or long:

double d = 10.1;      
String rounded = (false ? 0 : Math.round(d)) + ""; 

What happens (not a rhetorical question since I'm no where near a Java compiler)?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文