简体   繁体   中英

Math.ceil not rounding the value

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.

Surely you need:

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

or just:

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

To get the two parts you can use

int totalInches = (int) (d * 0.3937);
int feetPart = totalInches / 12;
int feetInchPart = totalInches % 12;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM