Java中一些函数的区别。

huangapple go评论68阅读模式
英文:

Differences between some functions in Java

问题

我是新手学习Java,我之前使用Python。我对使用Math.round(VALUE)函数和将值强制转换为整数(int) VALUE之间的区别很感兴趣。在Python中,将值强制转换为整数的等效方式是int(VALUE),所以我很难理解Java中的区别。有人可以解释一下这个区别吗?

英文:

I am new to Java and coming from a background in Python. I am curious about the difference between using the Math.round(VALUE) function versus casting to an integer like (int) VALUE. In Python, the equivalent to casting to an integer is int(VALUE), so I am struggling to understand the distinction in Java. Can someone explain this difference to me?

答案1

得分: 1

  1. 如果您正在解析Python中的字符串,您将调用 int(str),但在Java中,您将需要使用包装类型的静态解析方法,即 Integer.class 的静态方法,例如 Integer.parseInt(str)
# Python
str_val = '42'
int_val = int(str_val) # 42
print(type(str_val))   # <class 'str'>
print(type(int_val))   # <class 'int'>
/* Java */
String strVal = "42";
int intVal = Integer.parseInt(strVal); // 42
  1. 如果您要在Python中将浮点数转换为整数,您将调用 int(floating_value),但在Java中,您只需在值之前放置所需的类型,即 (int),例如 (int) floatingValue
# Python
floating_value = 3.14
int_val = int(floating_value) # 3
print(type(floating_value))   # <class 'float'>
print(type(int_val))          # <class 'int'>
/* Java */
float floatingVal = 3.14;
int intVal = (int) floatingVal; // 3

Python对类型转换和解析处理方式非常相似。

英文:

It depends on how you want to "cast".

  1. If you are parsing a string in Python, you would call int(str), but in Java you would have to use the boxed type's (Integer.class) static parsing method i.e Integer.parseInt(str)

    # Python
    str_val = &#39;42&#39;
    int_val = int(str_val) # 42
    print(type(str_val))   # &lt;class &#39;str&#39;&gt;
    print(type(int_val))   # &lt;class &#39;int&#39;&gt;
    
    /* Java */
    String strVal = &quot;42&quot;;
    int intVal = Integer.parseInt(strVal); // 42
    
  2. If you are converting a floating-point in Python, you would call int(floating_value), but in Java you would simply place the desired type i.e. (int) before the value i.e. (int) floatingValue.

    # Python
    floating_value = 3.14
    int_val = int(floating_value) # 3
    print(type(floating_value))   # &lt;class &#39;float&#39;&gt;
    print(type(int_val))          # &lt;class &#39;int&#39;&gt;
    
    /* Java */
    float floatingVal = 3.14;
    int intVal = (int) floatingVal; // 3
    

Python treats typecasting and parsing very similarly.

答案2

得分: 1

以下是您要翻译的内容:

"... In Python, the equivalent to casting to an integer is int(VALUE) ..."
在Python中,将值转换为整数的等效方式是 int(VALUE)

The Math#round function may not be the procedure you're looking for.
Math#round_ 函数可能不是您要寻找的过程。

In Java, you can cast a String value to an int value using the Integer#parseInt method.
在Java中,您可以使用 Integer#parseInt 方法将 String 值转换为 int 值。

int value = Integer.parseInt(&quot;123&quot;);
int value = Integer.parseInt("123");

"... I am curious about the difference between using the Math.round(VALUE) function versus casting to an integer like (int) VALUE ..."
“…我想了解使用 Math.round(VALUE) 函数与像 (int) VALUE 这样强制转换为整数之间的区别…”

If you are speaking in terms of a rounding mode, then the Math#round method is going to round towards positive infinity.
如果您考虑舍入模式,那么 Math#round 方法会向正无穷方向舍入。

Consider the following demonstration.
考虑以下演示。

double[] values = { 5.5, 2.5, 1.6, 1.1, 1.0, -1.0, -1.1, -1.6, -2.5, -5.5 };
for (double value : values)
    System.out.printf(&quot;%4.1f to %2d%n&quot;, value, Math.round(value));
double[] values = { 5.5, 2.5, 1.6, 1.1, 1.0, -1.0, -1.1, -1.6, -2.5, -5.5 };
for (double value : values)
    System.out.printf("%4.1f to %2d%n", value, Math.round(value));
 5.5 to  6
 2.5 to  3
 1.6 to  2
 1.1 to  1
 1.0 to  1
-1.0 to -1
-1.1 to -1
-1.6 to -2
-2.5 to -2
-5.5 to -5

Whereas, a cast to an int is going to simply truncate the fractional value.
而将其强制转换为 int 会简单地截断小数部分。
No rounding will occur.
不会发生舍入。

double[] values = { 5.5, 2.5, 1.6, 1.1, 1.0, -1.0, -1.1, -1.6, -2.5, -5.5 };
for (double value : values)
    System.out.printf(&quot;%4.1f to %2d%n&quot;, value, (int) value);
double[] values = { 5.5, 2.5, 1.6, 1.1, 1.0, -1.0, -1.1, -1.6, -2.5, -5.5 };
for (double value : values)
    System.out.printf("%4.1f to %2d%n", value, (int) value);
 5.5 to  5
 2.5 to  2
 1.6 to  1
 1.1 to  1
 1.0 to  1
-1.0 to -1
-1.1 to -1
-1.6 to -1
-2.5 to -2
-5.5 to -5
英文:

To preface, you mention the following.

> "... In Python, the equivalent to casting to an integer is int(VALUE) ..."

The Math#round function may not be the procedure you're looking for.

In Java, you can cast a String value to an int value using the Integer#parseInt method.

int value = Integer.parseInt(&quot;123&quot;);

> "... I am curious about the difference between using the Math.round(VALUE) function versus casting to an integer like (int) VALUE ..."

If you are speaking in terms of a rounding mode, then the Math#round method is going to round towards positive infinity.

Consider the following demonstration.

double[] values = { 5.5, 2.5, 1.6, 1.1, 1.0, -1.0, -1.1, -1.6, -2.5, -5.5 };
for (double value : values)
    System.out.printf(&quot;%4.1f to %2d%n&quot;, value, Math.round(value));
 5.5 to  6
 2.5 to  3
 1.6 to  2
 1.1 to  1
 1.0 to  1
-1.0 to -1
-1.1 to -1
-1.6 to -2
-2.5 to -2
-5.5 to -5

Whereas, a cast to an int is going to simply truncate the fractional value.
No rounding will occur.

double[] values = { 5.5, 2.5, 1.6, 1.1, 1.0, -1.0, -1.1, -1.6, -2.5, -5.5 };
for (double value : values)
    System.out.printf(&quot;%4.1f to %2d%n&quot;, value, (int) value);
 5.5 to  5
 2.5 to  2
 1.6 to  1
 1.1 to  1
 1.0 to  1
-1.0 to -1
-1.1 to -1
-1.6 to -1
-2.5 to -2
-5.5 to -5

huangapple
  • 本文由 发表于 2023年6月16日 03:31:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76484983.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定