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

时间:2022-06-21 16:34:17

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:

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

    String newValue = percentageValue.toString();

    String newValue = percentageValue.toString();

  • I have tried using the 'valueOf' method on the String object, as per below:

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

    String total2 = String.valueOf(percentageValue);

    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

16 个解决方案

#1


37  

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)。

#2


37  

Nice and simple. Add this snippet in whatever you're outputting to:

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

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

#3


13  

I did this to remove the decimal places from the double value

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

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

The output of the above is

以上的输出是

100

#4


12  

You could use

你可以用

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

Or

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

#5


8  

You can convert double,float variables to integer in a single line of code using explicit type casting.

您可以使用显式类型转换将double,float变量转换为单行代码中的整数。

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

The output will be 3

输出为3

#6


5  

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]);

#7


1  

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

#8


1  

String truncatedValue = String.format("%f", percentageValue).split("\\.")[0]; solves the purpose

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

The problem is two fold-

问题是双重的 -

  1. To retain the integral (mathematical integer) part of the double. Hence can't typecast (int) percentageValue
  2. 保留double的积分(数学整数)部分。因此不能强制转换(int)percentageValue

  3. Truncate (and not round) the decimal part. Hence can't use String.format("%.0f", percentageValue) or new java.text.DecimalFormat("#").format(percentageValue) as both of these round the decimal part.
  4. 截断(而不是舍入)小数部分。因此不能使用String.format(“%。0f”,percentageValue)或新的java.text.DecimalFormat(“#”)。format(percentageValue)作为这两个小数部分。

#9


1  

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));

#10


1  

Type casting to integer may create problem but even long type can not hold every bit of double after narrowing down to decimal places. If you know your values will never exceed Long.MAX_VALUE value, this might be a clean solution.

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

So use the following with the above known risk.

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

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

#11


1  

Alternatively, you can use the method int integerValue = (int)Math.round(double a);

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

#12


1  

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);
}

#13


1  

Double d = 1000d;
System.out.println("Normal value :"+d);
System.out.println("Without decimal points :"+d.longValue());

#14


0  

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();

#15


0  

Try:

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

#16


0  

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类。此类提供了许多格式化数字的功能。要获得double值作为字符串,没有小数,请使用下面的代码。

DecimalFormat decimalFormat = new DecimalFormat(".");
decimalFormat.setGroupingUsed(false);
decimalFormat.setDecimalSeparatorAlwaysShown(false);

String year = decimalFormat.format(32024.2345D);

#1


37  

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)。

#2


37  

Nice and simple. Add this snippet in whatever you're outputting to:

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

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

#3


13  

I did this to remove the decimal places from the double value

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

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

The output of the above is

以上的输出是

100

#4


12  

You could use

你可以用

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

Or

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

#5


8  

You can convert double,float variables to integer in a single line of code using explicit type casting.

您可以使用显式类型转换将double,float变量转换为单行代码中的整数。

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

The output will be 3

输出为3

#6


5  

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]);

#7


1  

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

#8


1  

String truncatedValue = String.format("%f", percentageValue).split("\\.")[0]; solves the purpose

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

The problem is two fold-

问题是双重的 -

  1. To retain the integral (mathematical integer) part of the double. Hence can't typecast (int) percentageValue
  2. 保留double的积分(数学整数)部分。因此不能强制转换(int)percentageValue

  3. Truncate (and not round) the decimal part. Hence can't use String.format("%.0f", percentageValue) or new java.text.DecimalFormat("#").format(percentageValue) as both of these round the decimal part.
  4. 截断(而不是舍入)小数部分。因此不能使用String.format(“%。0f”,percentageValue)或新的java.text.DecimalFormat(“#”)。format(percentageValue)作为这两个小数部分。

#9


1  

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));

#10


1  

Type casting to integer may create problem but even long type can not hold every bit of double after narrowing down to decimal places. If you know your values will never exceed Long.MAX_VALUE value, this might be a clean solution.

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

So use the following with the above known risk.

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

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

#11


1  

Alternatively, you can use the method int integerValue = (int)Math.round(double a);

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

#12


1  

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);
}

#13


1  

Double d = 1000d;
System.out.println("Normal value :"+d);
System.out.println("Without decimal points :"+d.longValue());

#14


0  

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();

#15


0  

Try:

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

#16


0  

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类。此类提供了许多格式化数字的功能。要获得double值作为字符串,没有小数,请使用下面的代码。

DecimalFormat decimalFormat = new DecimalFormat(".");
decimalFormat.setGroupingUsed(false);
decimalFormat.setDecimalSeparatorAlwaysShown(false);

String year = decimalFormat.format(32024.2345D);