java 如何四舍五入到最接近的 50 或 100?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25438203/
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-11-02 08:02:30  来源:igfitidea点击:

how to round to nearest 50 or 100?

javagroovy

提问by nightingale2k1

I have numbers (in decimal) I want to round it to "nearest 50":

我有数字(十进制)我想把它四舍五入到“最接近的 50”:

  1. 122 => round to 150
  2. 177 => round to 200
  3. 157 => round to 200
  4. 57 => round to 100;
  5. 37 => round to 50;
  6. 1557 => round to 1600
  7. 3537 => round to 3550
  1. 122 => 舍入到 150
  2. 177 => 舍入到 200
  3. 157 => 舍入到 200
  4. 57 => 舍入到 100;
  5. 37 => 舍入到 50;
  6. 1557 => 舍入到 1600
  7. 3537 => 舍入到 3550

how to do that with java/groovy ?

如何用 java/groovy 做到这一点?

回答by malisper

I believe djechlin's solution is almost right, he just left out some parts.

我相信 djechlin 的解决方案几乎是正确的,他只是遗漏了一些部分。

(x+49)/50 * 50;

(x+49)/50 * 50;

This works, because in Java, the result of integer division is an integer (the result is truncated). To explain for me, here are your examples with an additional example for a number which is already a multiple of 50:

这是有效的,因为在 Java 中,整数除法的结果是一个整数(结果被截断)。为我解释一下,以下是您的示例以及一个已经是 50 倍数的数字的附加示例:

122   + 49 => 171   / 50 => 3   * 50 => 150
177   + 49 => 226   / 50 => 4   * 50 => 200
157   + 49 => 206   / 50 => 4   * 50 => 200
57    + 49 => 106   / 50 => 2   * 50 => 100
37    + 49 => 86    / 50 => 1   * 50 => 50
1557  + 49 => 1606  / 50 => 32  * 50 => 1600
3537  + 49 => 3586  / 50 => 71  * 50 => 3550
150   + 49 => 199   / 50 => 3   * 50 => 150

回答by dmahapatro

Groovy x + (50 - (x % 50 ?: 50)):

常规x + (50 - (x % 50 ?: 50))

def round50up = { int x ->
    x + ( 50 - ( x % 50 ?: 50 ) )
}

assert round50up( 122  ) == 150 
assert round50up( 177  ) == 200 
assert round50up( 157  ) == 200 
assert round50up( 57   ) == 100 
assert round50up( 37   ) == 50 
assert round50up( 1557 ) == 1600 
assert round50up( 3537 ) == 3550 
assert round50up( 100  ) == 100 
assert round50up( 200  ) == 200 
assert round50up( 250  ) == 250

Test it here.

测试一下here

回答by Le Green

Hi I made this script for you

嗨,我为你制作了这个脚本

function roundToNearest(x) {
    if (x%50 < 25) {
        return x - (x%50); 
    }
    else if (x%50 > 25) {
        return x + (50 - (x%50)); 
    }
    else if (x%50 == 25) {
        return x + 25; //when it is halfawy between the nearest 50 it will automatically round up, change this line to 'return x - 25' if you want it to automatically round down
    }        
}

console.log(roundToNearest(701));

I tested it and it successfully rounds to the nearest 50, u can run it here to test it.

我测试了它,它成功地四舍五入到最接近的 50,你可以在这里运行它来测试它。

http://repl.it/languages/JavaScript

http://repl.it/languages/JavaScript

回答by fredjam

To round to the nearest 50 (up or down). you can convert to float divide by the amount you want to round into, use Math.round then multiply back up:

四舍五入到最接近的 50(向上或向下)。您可以转换为浮点数除以要舍入的数量,使用 Math.round 然后乘以备份:

    int orgVal = 122;
    Math.round((float)orgVal/50f)*50;

This only rounds, if you want to raise it to the next 50 which matches your examples, you can do the same thing but use the Math.ceil method

这只是回合,如果您想将其提高到与您的示例相匹配的下 50 个,您可以执行相同的操作,但使用 Math.ceil 方法

    int orgVal = 122;
    (int)Math.ceil((float)orgVal/50f)*50;

回答by djechlin

(x+25)/50*50

(x+25)/50*50

My answer is too short because I actually posted the simplest way to do this, so you also get this sentence.

我的答案太短了,因为我实际上发布了最简单的方法来做到这一点,所以你也得到了这句话。

Edit: I'm answering the question you asked, not what your examples illustrate. Leaving this answer in case anyone else has your question and tries to read the answers.

编辑:我正在回答你提出的问题,而不是你的例子所说明的。留下这个答案,以防其他人有你的问题并试图阅读答案。

回答by jorrin

You're rounding to the nearest multiple of 50 or 100 i might think. Try:

你四舍五入到我可能认为的最接近的 50 或 100 的倍数。尝试:

x is your input.

x 是您的输入。

int roundTo = 0; // this will serve as the value to round off to  
if(x%50 == 0){
 roundTo = x; // do nothing as x is already a multiple of 50
}
else{
roundTo = ((x/50) + 1) * 50; 
}

then use this to get the rounded value:

然后使用它来获得四舍五入的值:

x = x + (roundTo - x); 

example:

例子:

x is 49;
roundTo = 50 // (49/50 +1) * 50
x = 49 + (50 - 49)
x = 50

  x = 160
 roundTo = 200 // (160/50 + 1) * 50
 x = 160 + (200 - 160)
 x = 160