-(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];
}
介紹一下參數(shù):
price:需要處理的數(shù)字躏精,
position:保留小數(shù)點第幾位,
然后調(diào)用
float s =0.126;
NSString *sb = [self notRounding:s afterPoint:2];
NSLog(@"sb = %@",sb);
輸出結(jié)果為:sb = 0.12
接下來介紹NSDecimalNumberHandler
初始化時的關(guān)鍵參數(shù):decimalNumberHandlerWithRoundingMode:NSRoundDown
端蛆,
NSRoundDown代表的就是 只舍不入配乓。
scale的參數(shù)position代表保留小數(shù)點后幾位雏逾。