- (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);
輸出結(jié)果為:sb = 0.12
接下來介紹NSDecimalNumberHandler初始化時的關(guān)鍵參數(shù):decimalNumberHandlerWithRoundingMode:NSRoundDown汰瘫,
NSRoundDown代表的就是只舍不入桂塞。
scale的參數(shù)position代表保留小數(shù)點后幾位。