对字符串截取小数点后的指定位数(只舍不入)

//对字符串截取小数点后的指定位数(只舍不入)  price:需要处理的数字,position:保留小数点第几位

-(NSString*)interceptString:(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 = [ouncesDecimaldecimalNumberByRoundingAccordingToBehavior:roundingBehavior];

return  [NSStringstringWithFormat:@"%@",roundedOunces];

}

你可能感兴趣的:(对字符串截取小数点后的指定位数(只舍不入))