這個基本能夠確保最后是四舍五入并且能保留兩位小數(shù)。
-(float)roundFloat:(float)price{
return (floorf(price*100 + 0.5))/100;
}
其它參考:
//格式話小數(shù) 四舍五入類型 (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];
}