Java 使用 Math.round 四舍五入到最接近的十分之一?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19461213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:26:54  来源:igfitidea点击:

Using Math.round to round to the nearest tenth?

javamath

提问by Travis M

I just wanted to ask a quick question regarding the Math.round method. I'm trying to compute the division of two ints into a double. The equation looks like this: 199/39. When I do this it returns 5.0 as the answer. I know the answer should be 5.1 with some more decimals. I have been told to use the Math.round method to round it to the nearest tenth, but I have no idea how to accomplish this. Should I change that double variable to a int and make it int/int=int? I'm not sure how Math.round even works to get 5.1 as I've read it only rounds to the nearest integer not decimal point. Any help would be fantastic.

我只是想问一个关于 Math.round 方法的快速问题。我正在尝试将两个整数的除法计算为双精度。等式如下所示:199/39。当我这样做时,它返回 5.0 作为答案。我知道答案应该是 5.1,再加上一些小数。有人告诉我使用 Math.round 方法将其四舍五入到最接近的十分之一,但我不知道如何实现这一点。我应该将该双变量更改为 int 并使其成为 int/int=int 吗?我不确定 Math.round 是如何获得 5.1 的,因为我读过它只四舍五入到最接近的整数而不是小数点。任何帮助都会很棒。

P.S This is homework, but I ask only because I can't find any information in my notes, slides, or book on how to use Math.round.

PS 这是作业,但我问这个只是因为我在我的笔记、幻灯片或书中找不到任何关于如何使用 Math.round 的信息。

回答by Michael Yaworski

Your specific answer:

您的具体回答:

roundedNumber = (double)Math.round(unRoundedNumber * 10) / 10;

In general, the equation is:

一般来说,等式是:

roundedNumber = 
(double)Math.round(unRoundedNumber * Math.pow(10, digitsToRoundTo)) 
/ Math.pow(10, digitsToRoundTo);

回答by hexacyanide

You don't need Math.round()to get a resultant decimal value. If you divide an intby an int, you will get an int. if you want a decimal, then cast doubleto one of the input values. Then you will get a doubleas a result.

您不需要Math.round()获得结果的十进制值。如果将 anint除以 an int,您将得到 an int。如果您想要小数,则double转换为输入值之一。然后你会得到double一个结果。

(double) 199 / 39
199.0 / 39

// both return
5.102564102564102

回答by user207421

I know the answer should be 5.1 with some more decimals.

我知道答案应该是 5.1,再加上一些小数。

Not with integer division it shouldn't. 5 is correct.

不是整数除法,它不应该。5是正确的。

If you want a floating-point answer, you need to provide at least one floating-point operand, e.g. 199/39.0.

如果你想要一个浮点答案,你需要提供至少一个浮点操作数,例如199/39.0.

You can then format that for printing with as many or few decimal places you like, with System.printf()or DecimalFormat.

然后,您可以格式化它以使用您喜欢的任意多或少的小数位进行打印,使用System.printf()DecimalFormat.

You can't round the floating-point value itself to decimal places, because it doesn't have decimal places, it has binary places.

您不能将浮点值本身四舍五入到小数位,因为它没有小数位,它有二进制位。

See this questionfor a full discussion, especially my answer there.

请参阅此问题以进行全面讨论,尤其是我在那里的回答。

回答by Travis M

Thanks for all the help. After reading your posts and looking at it some more I ended up doing ratio=((double)199/39) and then going ratio=(double)Math.round(ratio*10)/10. Doing that got me the 5.1 i was looking for.

感谢所有的帮助。在阅读了您的帖子并查看了更多内容后,我最终执行了 ratio=((double)199/39) 然后执行 ratio=(double)Math.round(ratio*10)/10。这样做让我得到了我正在寻找的 5.1。