开发者

obj-c : iphone programming disabling the round off of a number

开发者 https://www.devze.com 2022-12-28 16:08 出处:网络
i need to show a number to a user : 1.96666777 (timeHour) moduloformat = [NSString stringWithFormat:@\"%0.0f hours \",timeHour];

i need to show a number to a user : 1.96666777 (timeHour)

moduloformat = [NSString stringWithFormat:@"%0.0f hours ",timeHour];

but it is rounding off the number automaticly. It gives "2" and i want to show "1" i could us开发者_StackOverflow中文版e a NSrange and take the first number but i wanna know how to deal with this. Thanks!!


moduloformat = [NSString stringWithFormat:@"%0.0f hours ",floor( timeHour )];


Easiest to just cast the float to an int. When you do that the decimal places get chopped off and no rounding occurs.

moduloformat = [NSString stringWithFormat:@"%i hours ", (int)timeHour];
0

精彩评论

暂无评论...
验证码 换一张
取 消