如何从 Java 中的“double”类型的值中删除十进制值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19060406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove decimal values from a value of type 'double' in Java
提问by Sudhir
I am invoking a method called "calculateStampDuty", which will return the amount of stamp duty to be paid on a property. The percentage calculation works fine, and returns the correct value of "15000.0". However, I want to display the value to the front end user as just "15000", so just want to remove the decimal and any preceding values thereafter. How can this be done? My code is below:
我正在调用一个名为“calculateStampDuty”的方法,该方法将返回要为房产支付的印花税金额。百分比计算工作正常,并返回“15000.0”的正确值。但是,我想向前端用户显示该值仅为“15000”,因此只想删除小数点和此后的任何前面的值。如何才能做到这一点?我的代码如下:
float HouseValue = 150000;
double percentageValue;
percentageValue = calculateStampDuty(10, HouseValue);
private double calculateStampDuty(int PercentageIn, double HouseValueIn){
double test = PercentageIn * HouseValueIn / 100;
return test;
}
I have tried the following:
我尝试了以下方法:
Creating a new string which will convert the double value to a string, as per below:
String newValue = percentageValue.toString();
I have tried using the 'valueOf' method on the String object, as per below:
String total2 = String.valueOf(percentageValue);
创建一个将双精度值转换为字符串的新字符串,如下所示:
String newValue = percentageValue.toString();
我尝试在 String 对象上使用“valueOf”方法,如下所示:
String total2 = String.valueOf(percentageValue);
However, I just cannot get a value with no decimal places. Does anyone know in this example how you would get "15000" instead of "15000.0"?
但是,我无法获得没有小数位的值。有谁知道在这个例子中你将如何得到“15000”而不是“15000.0”?
Thanks
谢谢
采纳答案by C-Otto
You can convert the double
value into a int
value.
int x = (int) y
where y is your double variable. Then, printing x
does not give decimal places (15000
instead of 15000.0
).
您可以将double
值转换为int
值。
int x = (int) y
其中 y 是您的双变量。然后,打印x
不会给出小数位(15000
而不是15000.0
)。
回答by federico-t
With a cast. You're basically telling the compiler "I know that I'll lose information with this, but it's okay". And then you convert the casted integer into a string to display it.
带演员表。您基本上是在告诉编译器“我知道这样做会丢失信息,但没关系”。然后将转换的整数转换为字符串以显示它。
String newValue = ((int) percentageValue).toString();
回答by Doorknob
You could use
你可以用
String newValue = Integer.toString((int)percentageValue);
Or
或者
String newValue = Double.toString(Math.floor(percentageValue));
回答by Jeff Lowery
Try:
尝试:
String newValue = String.format("%d", (int)d);
回答by senseiwu
You can use DecimalFormat, but please also note that it is not a good idea to use double in these situations, rather use BigDecimal
您可以使用 DecimalFormat,但也请注意,在这些情况下使用 double 不是一个好主意,而是使用 BigDecimal
回答by ederollora
I would try this:
我会试试这个:
String numWihoutDecimal = String.valueOf(percentageValue).split("\.")[0];
I've tested this and it works so then it's just convert from this string to whatever type of number or whatever variable you want. You could do something like this.
我已经测试过它并且它可以工作,所以它只是从这个字符串转换为你想要的任何类型的数字或任何变量。你可以做这样的事情。
int num = Integer.parseInt(String.valueOf(percentageValue).split("\.")[0]);
回答by raghavsood33
String truncatedValue = String.format("%f", percentageValue).split("\\.")[0];
solves the purpose
String truncatedValue = String.format("%f", percentageValue).split("\\.")[0];
解决目的
The problem is two fold-
问题是两方面的——
- To retain the integral (mathematical integer) part of the double. Hence can't typecast
(int) percentageValue
- Truncate (and not round) the decimal part. Hence can't use
String.format("%.0f", percentageValue)
ornew java.text.DecimalFormat("#").format(percentageValue)
as both of these round the decimal part.
- 保留double的整数(数学整数)部分。因此不能类型转换
(int) percentageValue
- 截断(而不是舍入)小数部分。因此不能使用
String.format("%.0f", percentageValue)
或new java.text.DecimalFormat("#").format(percentageValue)
两者都使用小数部分。
回答by Raghavendra
Try this you will get a string from the format method.
试试这个,你会从 format 方法中得到一个字符串。
DecimalFormat df = new DecimalFormat("##0");
df.format((Math.round(doubleValue * 100.0) / 100.0));
回答by George Siggouroglou
The solution is by using DecimalFormat class. This class provides a lot of functionality to format a number.
To get a double value as string with no decimals use the code below.
解决方案是使用DecimalFormat 类。此类提供了许多格式化数字的功能。
要获取双精度值作为没有小数的字符串,请使用以下代码。
DecimalFormat decimalFormat = new DecimalFormat(".");
decimalFormat.setGroupingUsed(false);
decimalFormat.setDecimalSeparatorAlwaysShown(false);
String year = decimalFormat.format(32024.2345D);
回答by Ajay Kumar Meher
Type casting to integermay create problem but even longtype can not hold every bit of double after narrowing down to decimal places. If you know your values will never exceed Long.MAX_VALUEvalue, this might be a clean solution.
将类型转换为整数可能会产生问题,但即使是long类型在缩小到小数位后也无法容纳 double 的每一位。如果您知道您的值永远不会超过Long.MAX_VALUE值,这可能是一个干净的解决方案。
So use the following with the above known risk.
因此,请使用具有上述已知风险的以下内容。
double mValue = 1234567890.123456;
long mStrippedValue = new Double(mValue).longValue();