最近项目中用到的小tip
保留两位小数不进行四舍五入
1).
Nslog("@.2f",floor((2.34567)*100)/100);
-(NSString *)notRounding:(float)price afterPoint:(int)position{
NSDecimalNumberHandler* roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:position raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
NSDecimalNumber *ouncesDecimal;
NSDecimalNumber *roundedOunces;
ouncesDecimal = [[NSDecimalNumber alloc] initWithFloat:price];
roundedOunces = [ouncesDecimal decimalNumberByRoundingAccordingToBehavior:roundingBehavior];
[ouncesDecimal release];
return [NSString stringWithFormat:@"%@",roundedOunces];
}
本文介绍了一种在编程中保留特定位数小数而不进行四舍五入的方法,通过使用Objective-C中的NSDecimalNumber和其相关方法实现。

6530

被折叠的 条评论
为什么被折叠?



