简体   繁体   English

Math.round() 的结果被忽略

[英]Result of Math.round() is ignored

Why is it not working?I want to "odleglosc" is 2.2 not 2.214354356为什么它不起作用?我想“odleglosc”是 2.2 不是 2.214354356

在此处输入图片说明

Math.round(argument) returns a number that is rounded from the argument. Math.round(argument)返回一个从参数四舍五入的数字。

In your example you ignore the returned value.在您的示例中,您忽略返回的值。

You probably meant to write:你可能想写:

 odleglosc = Math.round(odleglosc);
x = Math.round(x);

Otherwise if you just write Math.round(x);否则如果你只写Math.round(x); Java will make the calculation and have no variable to assign it to, and gets thrown away. Java 将进行计算并且没有变量可以分配给它,并被丢弃。

Math.round() does not modify your variable because a double the value is passed to the function (compare all-by-value vs call-by-reference). Math.round()不会修改您的变量,因为将值传递给函数的double (比较 all-by-value 与 call-by-reference)。

To round your value use四舍五入你的价值使用

a = Math.round(a);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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