对 double 进行舍入以将其转换为 int (java)

发布于 2024-08-28 19:29:51 字数 126 浏览 7 评论 0原文

现在我正在尝试这个:

int a = round(n);

其中 n 是一个 double 但它不起作用。我做错了什么?

Right now I'm trying this:

int a = round(n);

where n is a double but it's not working. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

苏辞 2024-09-04 19:29:51

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

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

因此,您必须转换返回值:

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

What is the return type of the round() method in the snippet?

If this is the Math.round() method, it returns a Long when the input param is Double.

So, you will have to cast the return value:

int a = (int) Math.round(doubleVar);
淤浪 2024-09-04 19:29:51

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

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

If you don't like Math.round() you can use this simple approach as well:

int a = (int) (doubleVar + 0.5);
淡笑忘祈一世凡恋 2024-09-04 19:29:51

双精度舍入为“最接近的”整数,如下所示:

1.4 -> 1

1.6 -> 2

-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)

Rounding double to the "nearest" integer like this:

1.4 -> 1

1.6 -> 2

-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;          
    }
}

You can change condition (result<0.5) as you prefer.

一笔一画续写前缘 2024-09-04 19:29:51

Math.round函数重载
当它收到一个 float 值时,它会给你一个 int 值。例如,这会起作用。

int a=Math.round(1.7f);

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

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

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

The Math.round function is overloaded
When it receives a float value, it will give you an int. For example this would work.

int a=Math.round(1.7f);

When it receives a double value, it will give you a long, therefore you have to typecast it to int.

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

This is done to prevent loss of precision. Your double value is 64bit, but then your int variable can only store 32bit so it just converts it to long, which is 64bit but you can typecast it to 32bit as explained above.

紫南 2024-09-04 19:29:51
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();
  }
}
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();
  }
}
又怨 2024-09-04 19:29:51
public static int round(double d) {
    if (d > 0) {
        return (int) (d + 0.5);
    } else {
        return (int) (d - 0.5);
    }
}
public static int round(double d) {
    if (d > 0) {
        return (int) (d + 0.5);
    } else {
        return (int) (d - 0.5);
    }
}
剑心龙吟 2024-09-04 19:29:51

Math.round 的文档说:

返回将参数舍入为整数的结果。结果
相当于 (int) Math.floor(f+0.5)

无需转换为int。也许与过去相比有所改变。

Documentation of Math.round says:

Returns the result of rounding the argument to an integer. The result
is equivalent to (int) Math.floor(f+0.5).

No need to cast to int. Maybe it was changed from the past.

牵强ㄟ 2024-09-04 19:29:51

您确实需要发布一个更完整的示例,以便我们可以看到您想要做什么。从您发布的内容中,我可以看到以下内容。首先,没有内置的 round() 方法。您需要调用 Math.round(n),或者静态导入 Math.round,然后像您一样调用它。

You really need to post a more complete example, so we can see what you're trying to do. From what you have posted, here's what I can see. First, there is no built-in round() method. You need to either call Math.round(n), or statically import Math.round, and then call it like you have.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文