方法一:
-(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];
[ouncesdecimal release];
return [nsstring stringwithformat:@"%@",roundedounces];
}
介绍一下参数:
price:需要处理的数字,
position:保留小数点第几位,
然后调用
float s =0.126;
nsstring *sv = [self notrounding:s afterpoint:2];
nslog(@"sv = %@",sv);
输出结果为:sv = 0.12
接下来介绍nsdecimalnumberhandler初始化时的关键参数:decimalnumberhandlerwithroundingmode:nsrounddown,
nsrounddown代表的就是 只舍不入。
scale的参数position代表保留小数点后几位。
如果只入不舍怎么办,比如,float 0.162 想要得到0.17该怎么做?,在开发文档上有这样一个表,是按照保留小数点后一位处理的。相信大家一看就明白了:

方法二:
1、round(12345.6789) 结果为:12346
2、round(12345.6789*100)/100 结果为:12345.68
第二个是我要的结果,但是我不明白这么个简单的四舍五入要搞的这么复杂,应该有更好的吧,我记得在其他语言里用:round(12345.6789,2) 就可以实现四舍五入到两位小数。