[java] How to remove decimal values from a value of type 'double' in Java

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

This question is related to java

The answer is


the simple way to remove

new java.text.DecimalFormat("#").format(value)

You could use

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

Or

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

Try:

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

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

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

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

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.

So use the following with the above known risk.

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

You can use DecimalFormat, but please also note that it is not a good idea to use double in these situations, rather use BigDecimal


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

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

The output of the above is

100


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

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

The output will be 3


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 = new DecimalFormat(".");
decimalFormat.setGroupingUsed(false);
decimalFormat.setDecimalSeparatorAlwaysShown(false);

String year = decimalFormat.format(32024.2345D);

declare a double value and convert to long convert to string and formated to float the double value finally replace all the value like 123456789,0000 to 123456789

Double value = double value ;
Long longValue = value.longValue();  String strCellValue1 = new String(longValue.toString().format("%f",value).replaceAll("\\,?0*$", ""));

Use Math.Round(double);

I have used it myself. It actually rounds off the decimal places.

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 

Hope it Helps :-)


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

This should do the trick.

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


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.

Try this you will get a string from the format method.

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

df.format((Math.round(doubleValue * 100.0) / 100.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+"";
        }

    }

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

Alternatively, you can use the method 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);
}