Java 总是四舍五入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19614695/
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
Always Round UP a Double
提问by sparklyllama
How could I always round up a double
to an int
, and never round it down.
I know of Math.round(double)
, but I want it to always round up.
So if it was 3.2
, it gets rounded to 4.
我怎么能总是将 a 舍入double
到 an int
,而不是将其舍入。我知道Math.round(double)
,但我希望它总是四舍五入。因此,如果是3.2
,则四舍五入为 4。
采纳答案by tabz100
You can use Math.ceil()
method.
您可以使用Math.ceil()
方法。
See JavaDoc link: https://docs.oracle.com/javase/10/docs/api/java/lang/Math.html#ceil(double)
请参阅 JavaDoc 链接:https://docs.oracle.com/javase/10/docs/api/java/lang/Math.html#ceil(double )
From the docs:
从文档:
ceil
细胞
public static double ceil(double a)
Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer. Special cases:
返回大于或等于参数且等于数学整数的最小(最接近负无穷大)双精度值。特别案例:
- If the argument value is already equal to a mathematical integer, then the result is the same as the argument.
- If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument.
- If the argument value is less than zero but greater than -1.0, then the result is negative zero.
- 如果参数值已经等于一个数学整数,则结果与参数相同。
- 如果参数是 NaN 或无穷大或正零或负零,则结果与参数相同。
- 如果参数值小于零但大于 -1.0,则结果为负零。
Note that the value of Math.ceil(x) is exactly the value of -Math.floor(-x).
请注意,Math.ceil(x) 的值正是 -Math.floor(-x) 的值。
Parameters:
参数:
- a - a value.
- a - 一个值。
Returns:
返回:
The smallest (closest to negative infinity) floating-point value that is greater than or equal to the argument and is equal to a mathematical integer.
大于或等于参数且等于数学整数的最小(最接近负无穷大)浮点值。
回答by Muhammad Ahmed
Math.ceil()
will give you the closest lowest value if you want it to be rounded to largest closest values you should use Math.floor()
Math.ceil()
如果您希望将其四舍五入到您应该使用的最大最接近值,将为您提供最接近的最低值 Math.floor()
回答by Lostkiefer
private int roundUP(double d){
double dAbs = Math.abs(d);
int i = (int) dAbs;
double result = dAbs - (double) i;
if(result==0.0){
return (int) d;
}else{
return (int) d<0 ? -(i+1) : i+1;
}
}
Good job ! ;)
做得好 !;)
回答by maral04
In simple words,
简单来说,
Math.ceil
will always round UPor as said above, in excess.Math.round
will round up or down depending on the decimals.- If the decimal is equal or higher than 5, then it's rounded up.
- decimal => 5. (1,5 = 2)
- If the decimal is less than 5, then it's rounded down.
- decimal < 5. (1,45 = 1)
- If the decimal is equal or higher than 5, then it's rounded up.
Math.ceil
将总是四舍五入或如上所述,超过。Math.round
将根据小数点向上或向下舍入。- 如果小数等于或大于 5,则四舍五入。
- 十进制 => 5. (1,5 = 2)
- 如果小数点小于 5,则四舍五入。
- 小数 < 5. (1,45 = 1)
- 如果小数等于或大于 5,则四舍五入。
Examples of Math.ceil
and Math.round
:
Math.ceil
和 的例子Math.round
:
The code Below would return:
Cost, without Ceil 2.2 and with Ceil 3 (int), 3.0 (double). If we round it: 2
下面的代码将返回:
Cost,没有Ceil 2.2 和Ceil 3 (int), 3.0 (double)。如果我们四舍五入:2
int m2 = 2200;
double rate = 1000.0;
int costceil = (int)Math.ceil(m2/rate);
double costdouble = m2/rate;
double costdoubleceil = Math.ceil(m2/rate);
int costrounded = (int)Math.round(m2/rate);
System.out.println("Cost, without Ceil "+costdouble+" and with Ceil "+
costceil+"(int), "+costdoubleceil+"(double). If we round it: "+costrounded);
If we change the value of m2 to for example 2499, the result would be:
Cost, without Ceil 2.499 and with Ceil 3 (int), 3.0 (double). If we round it: 2
If we change the value of m2 to for example 2550, the result would be:
Cost, without Ceil 2.55 and with Ceil 3 (int), 3.0 (double). If we round it: 3
如果我们将 m2 的值更改为例如2499,结果将是:成本,没有 Ceil 2.499 和 Ceil 3 (int), 3.0 (double)。如果我们四舍五入: 2
如果我们将 m2 的值更改为例如2550,则结果将是:
Cost,没有 Ceil 2.55 和 Ceil 3 (int), 3.0 (double)。如果我们四舍五入:3
Hope it helps. (Information extracted from previous answers, i just wanted to make it clearer).
希望能帮助到你。(从以前的答案中提取的信息,我只是想让它更清楚)。
回答by Tunji_D
My method is relatively simple, hope it works for you.
我的方法比较简单,希望对你有用。
In my case I have a row of objects that can only hold 3 items and I must adjust the number of rows I have to accommodate the items.
在我的情况下,我有一排只能容纳 3 个项目的对象,我必须调整我必须容纳这些项目的行数。
So I have some Double numberOfRows, I then use numberOfRows.intValue() to get an int value for numberOfRows.
所以我有一些双 numberOfRows,然后我使用 numberOfRows.intValue() 来获取 numberOfRows 的 int 值。
if the int value I get is less than numberOfRows, I add 1 to numberOfRows to round it up, else the value I get from numberOfRows.intValue() is the answer I want.
如果我得到的 int 值小于 numberOfRows,我将 numberOfRows 加 1 以将其四舍五入,否则我从 numberOfRows.intValue() 得到的值就是我想要的答案。
I wrote this simple for loop to test it out:
我写了这个简单的 for 循环来测试它:
for(int numberOfItems = 0; numberOfItems < 16; numberOfItems++) {
Double numberOfRows = numberOfItems / 3.0;
System.out.println("Number of rows are: " + numberOfRows);
System.out.println("Number of items are: " + numberOfItems);
if(numberOfRows.intValue() < numberOfRows) {
System.out.println("int number of rows are: " + (numberOfRows.intValue() + 1));
}
else {
System.out.println("int value of rows are: " + numberOfRows.intValue());
}
System.out.println();
System.out.println();
}
回答by trinity420
Short example without using Math.ceil().
不使用Math.ceil() 的简短示例。
public double roundUp(double d){
return d > (int)d ? (int)d + 1 : d;
}
Exaplanation:Compare operand to rounded down operand using typecast, if greater return rounded down argument + 1 (means round up) else unchanged operand.
说明:使用类型转换将操作数与向下舍入的操作数进行比较,如果更大则返回向下舍入的参数 + 1(表示向上舍入),否则返回未更改的操作数。
Example in Pseudocode
伪代码示例
double x = 3.01
int roundDown = (int)x // roundDown = 3
if(x > roundDown) // 3.01 > 3
return roundDown + 1 // return 3.0 + 1.0 = 4.0
else
return x // x equals roundDown
Anyway you should use Math.ceil()
. This is only meant to be a simple example of how you could do it by yourself.
无论如何你应该使用Math.ceil()
. 这只是一个简单的例子,说明如何自己做。
回答by Basil Bourque
tl;dr
tl;博士
BigDecimal( "3.2" ).setScale( 0 , RoundingMode.CEILING )
4
4
BigDecimal
BigDecimal
If you want accuracy rather than performance, avoid floating pointtechnology. That means avoiding float
, Float
, double
, Double
. For accuracy, use BigDecimal
class.
如果您想要准确性而不是性能,请避免使用浮点技术。这意味着避免float
, Float
, double
, Double
。为准确起见,请使用BigDecimal
类。
On a BigDecimal
, set the scale, the number of digits to the right of the decimal place. If you want no decimal fraction, set scale to zero. And specify a rounding mode. To always round an fraction upwards, use RoundingMode.CEILING
, documented as:
在 a 上BigDecimal
,设置 scale,即小数点右侧的位数。如果您不想要小数,请将比例设置为零。并指定一个舍入模式。要始终向上舍入分数,请使用RoundingMode.CEILING
,记录为:
Rounding mode to round towards positive infinity. If the result is positive, behaves as for RoundingMode.UP; if negative, behaves as for RoundingMode.DOWN. Note that this rounding mode never decreases the calculated value. So for example, 1.1 becomes 2, and your 3.2 becomes 4.
向正无穷大舍入的舍入模式。如果结果为正,则表现与 RoundingMode.UP 相同;如果为负,则与 RoundingMode.DOWN 的行为相同。请注意,此舍入模式永远不会减少计算值。例如,1.1 变为 2,而您的 3.2 变为 4。
BigDecimal bd = new BigDecimal( "3.2" ) ;
BigDecimal bdRounded = bd.setScale( 0 , RoundingMode.CEILING ) ;
String output = bdRounded.toString() ;
System.out.println( "bdRounded.toString(): " + bdRounded ) ; // 4
4
4
See this code run live at IdeOne.com.