java如何在双数据类型中删除逗号之后的数字

发布于 2025-02-02 16:23:13 字数 558 浏览 4 评论 0原文

因此,例如我拥有的和想要的,第一个数字是双重的,我希望它们成为整数,但没有零


2.00 - > 2


5.012-> 5


我尝试了这一点,但仍然无法正常工作的

if(result % 1 == 0){
    String temp = String.valueOf(result);
    int tempInt = Integer.parseInt(temp);
    tekst.setText(String.valueOf(tempInt));

    }else {
     tekst.setText(String.valueOf(result));
     num1 = 0;
     num2 = 0;
     result = 0;
}

结果变量是双重的, 此外,这也显示了

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "887.0"

so for example what I have and what I want it to, first numbers are double and I would like them to be integer but with no zeros


2.00 -> 2


5.012 -> 5


I tried with this, but still doesn't work

if(result % 1 == 0){
    String temp = String.valueOf(result);
    int tempInt = Integer.parseInt(temp);
    tekst.setText(String.valueOf(tempInt));

    }else {
     tekst.setText(String.valueOf(result));
     num1 = 0;
     num2 = 0;
     result = 0;
}

result variable is double,
also this shows after compiling

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "887.0"

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

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

发布评论

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

评论(5

白首有我共你 2025-02-09 16:23:13

有一个警告,您需要意识到,因为您尝试使用中间int actibal:

  • double> double值的范围是比的范围宽得多(显然, int )。因此,通过将double转换为long,然后再次转换为double,您可能会丢失数据。

以下是可以在不丢失数据的情况下完成的一些方法:

1。模块化除法:

double num = 59.012;

double wholeNum2 = num - num % 1;

2。静态方法 MATH。 floor()

double num = 59.012;

double wholeNum = Math.floor(num);

3。 base/java/text/decimalformat.html#方法 - 萨默里“ rel =” nofollow noreferrer“> decimalformat 类,可以相应地指定字符串模式并相应地格式化一个数字:

double num = 59.012;

NumberFormat format = new DecimalFormat("0");
double wholeNum = Double.parseDouble(format.format(num)); // parsing the formatted string

所有示例 :以上将为您提供输出59.0是您打印变量tholenum

当您需要获得double值时,将分数零件删除时,选项 1 2 是可取的。但是,代表此数字的字符串仍将包含一个点,最后一个零.0

但是,如果您需要将结果作为String仅包含double编号的整数part ,则decimalformat string.format()在注释中提到)将帮助您完全摆脱分数部分

NumberFormat format = new DecimalFormat("0");

System.out.println(format.format(59.012));

输出:

59

There is a caveat that you need to be aware about since you're trying to use an intermediate int variable:

  • The range of double values is far broader than the range of long (and int obviously). So by converting a double into a long and then again to double you might lose the data.

Here are some ways how it can be done without losing the data:

1. Modular division:

double num = 59.012;

double wholeNum2 = num - num % 1;

2. Static method Math.floor():

double num = 59.012;

double wholeNum = Math.floor(num);

3. DecimalFormat class, that allow to specify a string pattern and format a number accordingly:

double num = 59.012;

NumberFormat format = new DecimalFormat("0");
double wholeNum = Double.parseDouble(format.format(num)); // parsing the formatted string

All examples above will give you the output 59.0 is you print the variable wholeNum.

When you need to obtain a double value as a result with the fractional part dropped, options 1 and 2 are preferable. But a string representing this number will still contain a dot and one zero .0 at the end.

But if you need to get the result as a String containing only the integer part of a double number, then DecimalFormat (as well String.format() that was mentioned in the comments) will help you to get rid of the fractional part completely.

NumberFormat format = new DecimalFormat("0");

System.out.println(format.format(59.012));

Output:

59
穿越时光隧道 2025-02-09 16:23:13

这就是我要做的。

double num1 = 2.3;
double num2 = 4.5;

后来...

    int num11 = (int) num1;
    int num22 = (int) num2;
    System.out.println(num11 + ", " + num22 + ".");

结果将是:

2,4。

仅此而已。

This is what i would do.

double num1 = 2.3;
double num2 = 4.5;

Later...

    int num11 = (int) num1;
    int num22 = (int) num2;
    System.out.println(num11 + ", " + num22 + ".");

The result would be:

2, 4.

That's all.

天荒地未老 2025-02-09 16:23:13

您是否可能在用int解析的10.0001解析双字符串?因为它会认识到这不是INT并丢失错误。您应该首先将其解析为双重,然后将其施放给INT。

    //if you want to parse it from a string
    String doubleString = "100.0001";

    double parsedDoubleValue = Double.parseDouble(doubleString);
    System.out.println("Parsed double value: " + parsedDoubleValue);
    
    int castedValue = (int) parsedDoubleValue;
    System.out.println("Casted double value to int: " + castedValue);
    
    //if you have to original double
    double doubleValue = 1.0001;
    int intValue = (int)doubleValue;

    System.out.println(intValue);

Are you maybe parsing a double string so 10.0001 with an int parse? Because it will recognize that it's not an int and throw an error. You should first parse it to a double and then cast that to an int.

    //if you want to parse it from a string
    String doubleString = "100.0001";

    double parsedDoubleValue = Double.parseDouble(doubleString);
    System.out.println("Parsed double value: " + parsedDoubleValue);
    
    int castedValue = (int) parsedDoubleValue;
    System.out.println("Casted double value to int: " + castedValue);
    
    //if you have to original double
    double doubleValue = 1.0001;
    int intValue = (int)doubleValue;

    System.out.println(intValue);
马蹄踏│碎落叶 2025-02-09 16:23:13

使用(integer)(Math.floor(插入您的双重))
地板(双X)方法已经内置在数学类中,并返回双重,例如5.5变为5.0
然后,我们只是将该数字作为整数(或替代品),然后取出结果

use (Integer)(Math.floor(insert your double here))
The floor(double x) method is already built into the math class, and returns a double, for instance 5.5 becomes 5.0
Then we just instance that number as an Integer (or alternatively an int) and we get out result

画骨成沙 2025-02-09 16:23:13

根据parseint的方法文档(字符串):

Parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(String, int) method.
Params:
s – a String containing the int representation to be parsed
Returns:
the integer value represented by the argument in decimal.
Throws:
NumberFormatException – if the string does not contain a parsable integer.

数字是双重的,并且包含不可用的字符,即“”。因此,它会引起错误。

您可能会考虑:

int tempInt = Integer.parseInt(temp.split("\\.")[0]);

或直接跳过解析整数:

tekst.setText(temp.split("\\.")[0]);

According to the method documentation for parseInt(String s):

Parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(String, int) method.
Params:
s – a String containing the int representation to be parsed
Returns:
the integer value represented by the argument in decimal.
Throws:
NumberFormatException – if the string does not contain a parsable integer.

The number is a double and contains characters that are not parsable ie"." so it thows error.

You may consider:

int tempInt = Integer.parseInt(temp.split("\\.")[0]);

or just directly skip parsing the Integer:

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