这个基本能够确保最后是四舍五入并且能保留两位小数。
-(float)roundFloat:(float)price{
return (floorf(price*100 + 0.5))/100;
}
其它参考:
//格式话小数 四舍五入类型 (5舍4入)
..1..
-(NSString *) decimalwithFloatV:(float)floatV{
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setPositiveFormat:@"0.00"];
return [numberFormatter stringFromNumber:[NSNumber numberWithFloat:floatV]];
}
..2..
-(float)roundFloat:(float)price{
return roundf(price*100)/100;
}
..3..
-(float)roundFloat:(float)price{
NSString *temp = [NSString stringWithFormat:@"%.7f",price];
NSDecimalNumber *numResult = [NSDecimalNumber decimalNumberWithString:temp];
NSDecimalNumberHandler *roundUp = [NSDecimalNumberHandler
decimalNumberHandlerWithRoundingMode:NSRoundBankers
scale:2
raiseOnExactness:NO
raiseOnOverflow:NO
raiseOnUnderflow:NO
raiseOnDivideByZero:YES];
return [[numResult decimalNumberByRoundingAccordingToBehavior:roundUp] floatValue];
}