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
I did this to remove the decimal places from the double
value
new DecimalFormat("#").format(100.0);
The output of the above is
100
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();
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+"";
}
}
You could use
String newValue = Integer.toString((int)percentageValue);
Or
String newValue = Double.toString(Math.floor(percentageValue));
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
).