如何从Java中的’double’类型的值中删除十进制值

我正在调用一个名为“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; } 

我尝试过以下方法:

  • 创建一个新的字符串,将double值转换为字符串,如下所示:

    String newValue = percentageValue.toString();

  • 我尝试在String对象上使用’valueOf’方法,如下所示:

    String total2 = String.valueOf(percentageValue);

但是,我只是无法获得没有小数位的值。 在这个例子中有没有人知道如何获得“15000”而不是“15000.0”?

谢谢

您可以将double值转换为int值。 int x = (int) y其中y是你的双变量。 然后,打印x不会给出小数位( 15000而不是15000.0 )。

很好,很简单。 将此代码段添加到您输出的任何内容中:

 String.format("%.0f", percentageValue) 

我这样做是为了从double值中删除小数位

 new DecimalFormat("#").format(100.0); 

以上的输出是

100

你可以用

 String newValue = Integer.toString((int)percentageValue); 

要么

 String newValue = Double.toString(Math.floor(percentageValue)); 

您可以使用显式类型转换将doublefloat变量转换为单行代码中的integer

 float x = 3.05 int y = (int) x; System.out.println(y); 

输出为3

我会试试这个:

 String numWihoutDecimal = String.valueOf(percentageValue).split("\\.")[0]; 

我已经测试了这个并且它可以工作,所以它只是从这个字符串转换为任何类型的数字或你想要的任何变量。 你可以这样做。

 int num = Integer.parseInt(String.valueOf(percentageValue).split("\\.")[0]); 

你可以使用DecimalFormat,但请注意在这些情况下使用double不是一个好主意,而是使用BigDecimal

String truncatedValue = String.format("%f", percentageValue).split("\\.")[0]; 解决了目的

问题是双重的 –

  1. 保留double的积分(数学整数)部分。 因此不能强制转换(int) percentageValue
  2. 截断(而不是舍入)小数部分。 因此不能使用String.format("%.0f", percentageValue)new java.text.DecimalFormat("#").format(percentageValue)作为这两个小数部分。

试试这个你会从format方法中得到一个字符串。

 DecimalFormat df = new DecimalFormat("##0"); df.format((Math.round(doubleValue * 100.0) / 100.0)); 

类型转换为整数可能会产生问题,但即使类型在缩小到小数位后也无法保持每一位双精度。 如果您知道您的值永远不会超过Long.MAX_VALUE值,那么这可能是一个干净的解决方案。

因此,请使用以下已知风险。

 double mValue = 1234567890.123456; long mStrippedValue = new Double(mValue).longValue(); 

或者,您可以使用方法int integerValue = (int)Math.round(double a);

 Double i = Double.parseDouble("String with double value"); Log.i(tag, "display double " + i); try { NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(0); // set as you need String myStringmax = nf.format(i); String result = myStringmax.replaceAll("[-+.^:,]", ""); Double i = Double.parseDouble(result); int max = Integer.parseInt(result); } catch (Exception e) { System.out.println("ex=" + e); } 
 Double d = 1000d; System.out.println("Normal value :"+d); System.out.println("Without decimal points :"+d.longValue()); 

有演员。 你基本上是告诉编译器“我知道我会丢失这些信息,但它没关系”。 然后将转换后的整数转换为字符串以显示它。

 String newValue = ((int) percentageValue).toString(); 

尝试:

 String newValue = String.format("%d", (int)d); 

解决方案是使用DecimalFormat类 。 此类提供了许多格式化数字的function。
要获得double值作为字符串,没有小数,请使用下面的代码。

 DecimalFormat decimalFormat = new DecimalFormat("."); decimalFormat.setGroupingUsed(false); decimalFormat.setDecimalSeparatorAlwaysShown(false); String year = decimalFormat.format(32024.2345D);