1.四舍五入, 使用 %.2f 本身就是会四舍五入的.
2.对小数点后两位数会大于0即加1
//price:需要处理的数字,
//position:保留小数点第几位,
-(NSString *)stringByNotRounding:(double)price afterPoint:(int)position{
NSDecimalNumberHandler* roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundUp scale:position raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
NSDecimalNumber *ouncesDecimal;
NSDecimalNumber *roundedOunces;
ouncesDecimal = [[NSDecimalNumber alloc] initWithFloat:price];
roundedOunces = [ouncesDecimal decimalNumberByRoundingAccordingToBehavior:roundingBehavior];
return [NSString stringWithFormat:@"%@",roundedOunces];
}
针对NSDecimalNumber的解释:
http://www.ithao123.cn/content-8054207.html