一般我们用一个folat类型去设置text内容时常会遇到四舍五入!
那我们就需要去转换一下来解决此问题
写一个方法将folat类型转换成string
-(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];
return [NSString stringWithFormat:@"%@",roundedOunces];
}
然后再用的时候将folat穿进去
float number = 3.27681779;(假如)
NSString *string = [self notRounding:number afterPoint:2];
cell.backMoneyMonth.text = [NSString stringWithFormat:@"每月返款:¥%@",string];
这样可以解决四舍五入问题
但是:
例如.后面第二位为0时,则会去掉0显示为3.2
那我们在处理的时候再把它专为folat类型
cell.backMoneyMonth.text = [NSString stringWithFormat:@"每月返款:¥%.2f",[string floatValue]];

网友评论