数字四舍五入方案
方案1:
+ (NSString *)rounding:(float)value afterPoint:(int)position{
NSDecimalNumberHandler* round_handler = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode: NSRoundPlain scale:position raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
NSDecimalNumber * decimal_number = [[NSDecimalNumber alloc] initWithFloat:value];
NSDecimalNumber * round_number = [decimal_number decimalNumberByRoundingAccordingToBehavior:round_handler];
return [NSString stringWithFormat:@"%@",round_number];
}
参数:value:要四舍五入的小数
position:保留小数点后几位数字。
重要属性:
NSRoundPlain, //取整
NSRoundDown, // 只舍不入
NSRoundUp, // 只入不舍
NSRoundBankers //四舍五入
方案二
1、round(12345.6789) 结果为:12346
2、round(12345.6789*100)/100 结果为:12345.68