java如何在双数据类型中删除逗号之后的数字
因此,例如我拥有的和想要的,第一个数字是双重的,我希望它们成为整数,但没有零
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有一个警告,您需要意识到,因为您尝试使用中间
int
actibal:double> double
值的范围是比长
的范围宽得多(显然, int )。因此,通过将double
转换为long
,然后再次转换为double
,您可能会丢失数据。以下是可以在不丢失数据的情况下完成的一些方法:
1。模块化除法:
2。静态方法
MATH。 floor()
:3。 base/java/text/decimalformat.html#方法 - 萨默里“ rel =” nofollow noreferrer“>
decimalformat
类,可以相应地指定字符串模式并相应地格式化一个数字:所有示例 :以上将为您提供输出
59.0
是您打印变量tholenum
。当您需要获得
double
值时,将分数零件删除时,选项 1 和 2 是可取的。但是,代表此数字的字符串仍将包含一个点,最后一个零.0
。但是,如果您需要将结果作为
String
仅包含double
编号的整数part ,则decimalformat
(string.format()
在注释中提到)将帮助您完全摆脱分数部分。输出:
There is a caveat that you need to be aware about since you're trying to use an intermediate
int
variable:double
values is far broader than the range oflong
(andint
obviously). So by converting adouble
into along
and then again todouble
you might lose the data.Here are some ways how it can be done without losing the data:
1. Modular division:
2. Static method
Math.floor()
:3.
DecimalFormat
class, that allow to specify a string pattern and format a number accordingly:All examples above will give you the output
59.0
is you print the variablewholeNum
.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 adouble
number, thenDecimalFormat
(as wellString.format()
that was mentioned in the comments) will help you to get rid of the fractional part completely.Output:
这就是我要做的。
后来...
结果将是:
2,4。
仅此而已。
This is what i would do.
Later...
The result would be:
2, 4.
That's all.
您是否可能在用int解析的10.0001解析双字符串?因为它会认识到这不是INT并丢失错误。您应该首先将其解析为双重,然后将其施放给INT。
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.
使用(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
根据parseint的方法文档(字符串):
数字是双重的,并且包含不可用的字符,即“”。因此,它会引起错误。
您可能会考虑:
或直接跳过解析整数:
According to the method documentation for parseInt(String s):
The number is a double and contains characters that are not parsable ie"." so it thows error.
You may consider:
or just directly skip parsing the Integer: