0
double totalInches = d * 0.3937;
double feetPart = totalInches / 12;
int inchesPart = (int) Math.ceil(totalInches - (feetPart * 12));
return (feetPart) + "' " + inchesPart + "''";

I am getting a value 6.9999999 ' 0". I am returning a string, is it the reason why the decimals values in feet is not getting rounded off.

I tried without casting too. double inchesPart = Math.ceil(totalInches - (feetPart * 12));, but still i get the same result.

4

2 回答 2

5

你肯定需要:

int feetPart = (int)Math.floor(totalInches / 12);

要不就:

int feetPart = (int)(totalInches / 12);

于 2013-04-17T14:51:28.587 回答
1

要获得这两个部分,您可以使用

int totalInches = (int) (d * 0.3937);
int feetPart = totalInches / 12;
int feetInchPart = totalInches % 12;
于 2013-04-17T15:45:55.383 回答