68

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

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

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"?

Thanks

4

21 回答 21

81

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

String.format("%.0f", percentageValue)
于 2015-10-25T12:07:16.387 回答
49

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

于 2013-09-27T21:36:06.107 回答
43

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

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

上面的输出是

100

于 2016-05-07T13:31:39.657 回答
14

You could use

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

Or

String newValue = Double.toString(Math.floor(percentageValue));
于 2013-09-27T21:35:35.400 回答
8

您可以使用显式类型double转换将float变量转换integer为单行代码。

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

输出将是3

于 2016-05-11T10:21:07.977 回答
5

我会试试这个:

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

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

int num = Integer.parseInt(String.valueOf(percentageValue).split("\\.")[0]);
于 2014-03-21T12:36:28.413 回答
5
Double d = 1000d;
System.out.println("Normal value :"+d);
System.out.println("Without decimal points :"+d.longValue());
于 2017-07-10T03:12:16.460 回答
4

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

DecimalFormat df = new DecimalFormat("##0");

df.format((Math.round(doubleValue * 100.0) / 100.0));
于 2015-04-07T08:51:03.187 回答
3

使用 Math.Round(double);

我自己用过。它实际上四舍五入小数位。

d = 19.82;
ans = Math.round(d);
System.out.println(ans);
// Output : 20 

d = 19.33;
ans = Math.round(d);
System.out.println(ans);
// Output : 19 

希望能帮助到你 :-)

于 2020-04-30T05:07:05.517 回答
3

删除的简单方法

new java.text.DecimalFormat("#").format(value)
于 2021-02-23T15:31:49.390 回答
1

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

The problem is two fold-

  1. To retain the integral (mathematical integer) part of the double. Hence can't typecast (int) percentageValue
  2. 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.
于 2014-12-01T20:41:55.060 回答
1

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

于 2013-09-27T21:38:24.497 回答
1

解决方案是使用DecimalFormat 类。这个类提供了很多格式化数字的功能。
要获得不带小数的字符串形式的双精度值,请使用以下代码。

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

String year = decimalFormat.format(32024.2345D);
于 2015-07-13T13:51:14.337 回答
1

声明一个 double 值并转换为 long 转换为字符串并格式化为 float 值最后将所有值替换为 123456789,0000 到 123456789

Double value = double value ;
Long longValue = value.longValue();  String strCellValue1 = new String(longValue.toString().format("%f",value).replaceAll("\\,?0*$", ""));
于 2021-02-23T13:43:48.103 回答
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);
}
于 2016-09-29T05:25:44.927 回答
1

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

于 2016-04-11T19:41:00.433 回答
1

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

因此,使用以下具有上述已知风险的方法。

double mValue = 1234567890.123456;
long mStrippedValue = new Double(mValue).longValue();
于 2015-08-12T10:39:47.910 回答
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();
于 2013-09-27T21:35:23.327 回答
0
    public class RemoveDecimalPoint{

         public static void main(String []args){
            System.out.println(""+ removePoint(250022005.60));
         }
 
       public static String  removePoint(double number) {        
            long x = (long) number;
            return x+"";
        }

    }
于 2020-11-17T12:42:31.330 回答
0

这应该可以解决问题。

System.out.println(percentageValue.split("\.")[0]);

于 2020-03-19T22:38:36.010 回答
-1

尝试:

String newValue = String.format("%d", (int)d);
于 2013-09-27T21:37:48.897 回答