如何从Java中的'double'类型的值中删除十进制值 [英] How to remove decimal values from a value of type 'double' in Java
问题描述
我正在调用一个名为calculateStampDuty的方法,该方法将返回在物业上支付的
印花税金额。百分比计算工作
罚款,并返回正确的值15000.0。但是,我想将前端用户的
值显示为15000,所以只想删除小数和之后的任何前面的值
。如何才能做到这一点?我的代码如下:
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:
float HouseValue = 150000;
double percentageValue;
percentageValue = calculateStampDuty(10, HouseValue);
private double calculateStampDuty(int PercentageIn, double HouseValueIn){
double test = PercentageIn * HouseValueIn / 100;
return test;
}
我试过以下内容:
-
创建一个新的字符串,它将double值转换为字符串,如下所示:
Creating a new string which will convert the double value to a string, as per below:
String newValue = percentageValue.toString();
我试过在字符串上使用'valueOf'方法对象,如下所示:
I have tried using the 'valueOf' method on the String object, as per below:
String total2 = String.valueOf(percentageValue);
但是,我无法获得没有小数位的值。有没有人知道这个例子中的
如何获得15000而不是15000.0?
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"?
谢谢
推荐答案
您可以将 double
值转换为 int
值。
int x =(int)y
其中y是你的双变量。然后,打印 x
不会给出小数位( 15000
而不是 15000.0
)。
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
).
这篇关于如何从Java中的'double'类型的值中删除十进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!