将数字四舍五入到特定的倍数

编程入门 行业动态 更新时间:2024-10-13 18:23:43
本文介绍了将数字四舍五入到特定的倍数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我怎样才能在Java中找到特定的倍数? 在excel中有 mround 函数,它可以很容易地四舍五入到指定的倍数,如下所示:

How can I round to a specific multiple in Java? In excel there is the mround function which allows for easy rounding to a specified multiple like so:

mRound(variable,multiple)

所以<$ c $如果 x = 7.9 和,则返回 9 code> 6 if x = 7.2 。

so mRound(x,3) would return 9 if x = 7.9 and 6 if x = 7.2.

所有四舍五入我迄今为止发现的函数总是四舍五入到最接近的整数或指定的小数位数,但我希望能够更改每个变量的倍数。有没有人知道哪种功能最适合这种情况?

All of the rounding functions I have found so far always round to the nearest whole number or to a specified number of decimal places but I want to be able to change the multiple for each variable. Does anyone know what function would be best for this situation?

推荐答案

只需将数字除以数字,再乘以数字即可。

Just divide by the number, round, and multiply by the number.

double mRound(double value, double factor) { return Math.round(value / factor) * factor; }

更多推荐

将数字四舍五入到特定的倍数

本文发布于:2023-11-07 23:27:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1567724.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:倍数   四舍五入   数字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!