- (NSString*)notRounding:(CGFloat)price afterPoint:(NSInteger)position {
NSDecimalNumberHandler*roundingBehavior = [NSDecimalNumberHandlerdecimalNumberHandlerWithRoundingMode:NSRoundDownscale:positionraiseOnExactness:NOraiseOnOverflow:NOraiseOnUnderflow:NOraiseOnDivideByZero:NO];
NSDecimalNumber*ouncesDecimal;
NSDecimalNumber*roundedOunces;
ouncesDecimal = [[NSDecimalNumberalloc]initWithFloat:price];
roundedOunces = [ouncesDecimaldecimalNumberByRoundingAccordingToBehavior:roundingBehavior];
return[NSStringstringWithFormat:@"%@",roundedOunces];
}
使用方法:
floats =0.126;
NSString*sb = [selfnotRounding:s afterPoint:2];
NSLog(@"sb = %@",sb);
输出结果为:sb = 0.12
接下来介绍NSDecimalNumberHandler初始化时的关键参数:decimalNumberHandlerWithRoundingMode:NSRoundDown,
NSRoundDown代表的就是只舍不入。
scale的参数position代表保留小数点后几位。