Reputation: 1472
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
Upvotes: 80
Views: 460279
Reputation: 89
This should do the trick.
System.out.println(percentageValue.split("\\.")[0]);
Upvotes: 0
Reputation: 93
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*$", ""));
Upvotes: 1
Reputation: 93
the simple way to remove
new java.text.DecimalFormat("#").format(value)
Upvotes: 4
Reputation: 1
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+"";
}
}
Upvotes: 0
Reputation: 41
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 :-)
Upvotes: 4
Reputation: 6792
I did this to remove the decimal places from the double
value
new DecimalFormat("#").format(100.0);
The output of the above is
100
Upvotes: 52
Reputation: 51
Double d = 1000d;
System.out.println("Normal value :"+d);
System.out.println("Without decimal points :"+d.longValue());
Upvotes: 5
Reputation: 628
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);
}
Upvotes: 1
Reputation: 479
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
Upvotes: 10
Reputation: 331
Alternatively, you can use the method int integerValue = (int)Math.round(double a);
Upvotes: 1
Reputation: 1952
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();
Upvotes: 1
Reputation: 975
Nice and simple. Add this snippet in whatever you're outputting to:
String.format("%.0f", percentageValue)
Upvotes: 95
Reputation: 2303
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));
Upvotes: 5
Reputation: 19944
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);
Upvotes: 3
Reputation: 768
String truncatedValue = String.format("%f", percentageValue).split("\\.")[0];
solves the purpose
The problem is two fold-
(int) percentageValue
String.format("%.0f", percentageValue)
or new java.text.DecimalFormat("#").format(percentageValue)
as both of these round the decimal part.Upvotes: 1
Reputation: 1181
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]);
Upvotes: 4
Reputation: 59283
You could use
String newValue = Integer.toString((int)percentageValue);
Or
String newValue = Double.toString(Math.floor(percentageValue));
Upvotes: 16
Reputation: 5269
You can use DecimalFormat, but please also note that it is not a good idea to use double in these situations, rather use BigDecimal
Upvotes: 1
Reputation: 5843
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
).
Upvotes: 51
Reputation: 12341
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();
Upvotes: 1