2010-04-16 40 views
80

现在,我想这样的:四舍五入双把它变成一个int(JAVA)

int a = round(n); 

其中ndouble,但它不工作。我究竟做错了什么?

+0

可能的重复http://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java – 2010-04-16 17:09:53

+4

你应该详细说明“不工作”更多详情。怎么了?不会发生什么?你期望什么?你有什么错误?你在同一个类中有'round()'方法吗?你是否导入了静态java.lang.Math。*'?等等。有很多方法可以对数字进行舍入,因此也有很多可能的答案。换句话说,你的问题含糊不清,不能以目前的形式合理回答。它在黑暗中拍摄。 – BalusC 2010-04-16 17:09:55

+1

“不工作”是指不是舍入到最接近的整数,或抛出异常,或不是向上舍入/舍入?这个问题是无用的,而不必交叉参考上下文与答案。 – modulitos 2014-07-11 21:25:09

回答

181

代码段中round()方法的返回类型是什么?

如果这是Math.round()方法,则在输入参数为Double时返回Long。

所以,你将不得不返回值的类型转换:

int a = (int) Math.round(doubleVar); 
+0

考虑使用Math.toIntExact(long),而不是仅将其转换为int;一个double可以保存相当多的值,如果你的double比你想象的大,你可能不想默默地抛出最重要的位。 – othp 2018-03-01 16:38:43

3
import java.math.*; 
public class TestRound11 { 
    public static void main(String args[]){ 
    double d = 3.1537; 
    BigDecimal bd = new BigDecimal(d); 
    bd = bd.setScale(2,BigDecimal.ROUND_HALF_UP); 
    // output is 3.15 
    System.out.println(d + " : " + round(d, 2)); 
    // output is 3.154 
    System.out.println(d + " : " + round(d, 3)); 
    } 

    public static double round(double d, int decimalPlace){ 
    // see the Javadoc about why we use a String in the constructor 
    // http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html#BigDecimal(double) 
    BigDecimal bd = new BigDecimal(Double.toString(d)); 
    bd = bd.setScale(decimalPlace,BigDecimal.ROUND_HALF_UP); 
    return bd.doubleValue(); 
    } 
} 
-1

你真的需要发布一个更完整的例子,所以我们可以看到你想要做什么。从你发布的内容来看,这里是我能看到的。首先,没有内置的round()方法。您需要拨打Math.round(n)或静态导入Math.round,然后像您一样拨打电话。

+0

java中没有全局函数,只是开始。 – Danon 2017-03-04 10:27:19

12

四舍五入到 “最近” 整数这样的:

1.4 - >

1.6 - >

-2.1 - >-2

-1.3 - >-1

-1.5 - >- 2

private int round(double d){ 
    double dAbs = Math.abs(d); 
    int i = (int) dAbs; 
    double result = dAbs - (double) i; 
    if(result<0.5){ 
     return d<0 ? -i : i;    
    }else{ 
     return d<0 ? -(i+1) : i+1;   
    } 
} 

您可以根据需要更改条件(结果< 0.5)

+0

我同意anivaler的回答。但是,如果您需要将四舍五入为最大整数,您可以像下面这样: double decimalNumber = 4.56777; System.out.println(new Float(Math.round(decimalNumber))); 输出:5 – Smeet 2015-11-19 11:01:39

24

如果你不喜欢Math.round()你可以使用这个简单的方法,以及:

int a = (int) (doubleVar + 0.5); 
+1

显然没有不喜欢'Math.round()'的有价值的原因:http://stackoverflow.com/a/6468757/1715716 – 2016-02-09 22:02:48

+3

该解决方案更短,不需要导入并且便携到许多其他编程语言,只需很少的更改。 它可能会更快取决于您的平台:https://stackoverflow.com/questions/12091014/what-is-the-most-efficient-way-to-round-a-float-value-to-the -nearest-integer-in/12091343#12091343 – 2016-02-11 13:50:27

+0

我不是批评你的答案,我发现**非常有用**因为你提到的原因。通过这个评论,我解决了那些不敢使用'Math.round()'的人,这个人和你提供的解决方案一样“字面上”,就是这样。干杯。 – 2016-02-11 16:37:25

1

Math.round文档说:

返回四舍五入的参数结果一个整数。结果 相当于(int) Math.floor(f+0.5)

无需投射到int。也许它是从过去改变过来的。当它接收到一个浮点值

+3

有2个Math.round方法。采用浮点数返回一个整数,这是正确的。但需要双倍回报的人才会返回很长时间。 – 2015-04-24 21:19:46

0
public static int round(double d) { 
    if (d > 0) { 
     return (int) (d + 0.5); 
    } else { 
     return (int) (d - 0.5); 
    } 
} 
0

的Math.round函数被重载 ,它会给你一个int。例如,这将工作。

int a=Math.round(1.7f); 

当它收到一个double值时,它会给你一个很长的时间,因此你必须将它转换为int类型。

int a=(int)Math.round(1.7); 

这样做是为了防止精度损失。你的double值是64位,但是你的int变量只能存储32位,所以它只是把它转换为64位的long,但你可以如上所述将它转换为32位。