Reputation: 15034
public static String convertCentimeterToHeight(double d) {
double feetPart = 0;
double inchesPart = 0;
if (String.valueOf(d) != null && String.valueOf(d).trim().length() != 0) {
feetPart = (int) Math.floor((d / 2.54) / 12);
inchesPart = (int) Math.ceil((d / 2.54) - (feetPart * 12));
}
return (String.valueOf(feetPart)) + "' " + String.valueOf(inchesPart) + "''";
}
I am trying to remove decimals, but i am still getting something like this 5.0' 6.0"
. When we do a String.value() on a variable
does it remove the decimals and give me the exact number?
Upvotes: 0
Views: 278
Reputation: 533500
For your information.
// always true, it never returns null
String.valueOf(d) != null
// always true, a double is never an empty string.
String.valueOf(d).trim().length() != 0;
// same as
int feetPart = (int) (d / 2.54 / 12);
int inchesPart = ((int) (d / 2.54)) % 12;
// same as
return feetPart + "' " + inchesPart + "\"";
Upvotes: 1
Reputation: 58
all the answer replace your variable type. my way keeps. only use these codes you will find the magic.
double d = 1.0d;
String s = String.format("%1$.0f", d);
System.out.println(s);
Upvotes: 0
Reputation: 16351
As you want to store int
s in feetPart
and inchesPart
variables, just declare them as int
s :
int feetPart = 0;
int inchesPart = 0;
Then you'll avoid them being displayed as double
s (i.e. with the trailing .0
).
Upvotes: 5
Reputation: 321
You can cast the double variables to an int
return (String.valueOf((int)feetPart)) + "' " +
String.valueOf((int)inchesPart) + "''";
That should fix it as well.
Upvotes: 1