IOS 关于四舍五入的
来源:互联网 发布:怎么联系各大网络推手 编辑:程序博客网 时间:2024/05/03 17:40
如何只舍不入。比如 float price = 0.126,怎么样才能得到0.12?
当然,通过字符串截取的办法肯定也能达到相同的效果。但是就是这么一个简单的问题要通过一些判断和截取才能获得结果,总感觉有点笨拙。
下面先给出该问题的解决办法:
-(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) 就可以实现四舍五入到两位小数。
方法三:
NSTimeInterval Interval = 305.721125;
NSInteger timeInt = [[NSString stringWithFormat:@"%.0f", Interval] integerValue];
Interval:305.721125
timeInt:306
1
NSLog(@
"%@"
, [NSString stringWithFormat:@
"%.0f"
, 1.0003]);
2
NSLog(@
"%@"
, [NSString stringWithFormat:@
"%.0f"
, 1.9003]);
3
NSLog(@
"%@"
, [NSString stringWithFormat:@
"%.0f"
, 1.5003]);
4
NSLog(@
"%@"
, [NSString stringWithFormat:@
"%.0f"
, 1.4003]);
1
11
1
1
2
1
2
1
1
方法四:
1. /
//Test "/"
cout << "Test \"/\"!" << endl;
cout << "7 / 2 = " << 7/2 << endl; //3
cout << "7 / 2.0 = " << 7/2.0 << endl; //3.5
cout << "7.0 / 2 = " << 7.0/2 << endl; //3.5
cout << "7.0 / 2.0 = " << 7.0/2.0 << endl; //3.5
cout << "7 / 3 = " << 7/3 << endl; //2
cout << endl;
2. %
//Test "%"
cout << "Test \"%\"!" << endl;
cout << "9 % 3 = " << 9%3 << endl; //0
cout << "9 % 4 = " << 9%4 << endl; //1
//cout << "9.0 % 3 = " << 9.0%3 << endl;
//cout << "9 % 3.0 = " << 9%3.0 << endl;
cout << endl;
3. 四舍五入
//Test round()
cout << "Test \"四舍五入\"!" << endl;
double dRoundA = 1.4;
double dRoundB = 1.6;
double dRoundLowA = -1.4;
double dRoundLowB = -1.6;
double dRoundLowC = 0.0;
cout << dRoundA << " = " << RoundEx(dRoundA) << endl; //1
cout << dRoundB << " = " << RoundEx(dRoundB) << endl; //2
cout << dRoundLowA << " = " << RoundEx(dRoundLowA) << endl; //-1
cout << dRoundLowB << " = " << RoundEx(dRoundLowB) << endl; //-2
cout << dRoundLowC << " = " << RoundEx(dRoundLowC) << endl; //0
cout << endl;
double RoundEx(const double& dInput)
{
double dIn = dInput;
if (dInput >= 0.0) //???
{
return int(dIn + 0.5);
}
else
{
return int(dIn - 0.5);
}
}
4. ceil() 向上取整
//Test ceil() 向上取整
cout << "Test ceil() 向上取整!" << endl;
cout << "ceil 1.2 = " << ceil(1.2) << endl; //2
cout << "ceil 1.8 = " << ceil(1.8) << endl; //2
cout << "ceil -1.2 = " << ceil(-1.2) << endl; //-1
cout << "ceil -1.8 = " << ceil(-1.8) << endl; //-1
cout << "ceil 0.0 = " << ceil(0.0) << endl; //0
cout << endl;
5. floor() 向下取整
//Test floor() 向下取整
cout << "Test floor() 向下取整!" << endl;
cout << "floor 1.2 = " << floor(1.2) << endl; //1
cout << "floor 1.8 = " << floor(1.8) << endl; //1
cout << "floor -1.2 = " << floor(-1.2) << endl; //-2
cout << "floor -1.8 = " << floor(-1.8) << endl; //-2
cout << "floor 0.0 = " << floor(0.0) << endl; //0
cout << endl;
- IOS 关于四舍五入的
- iOS关于小数点的四舍五入
- 关于DELPHI的四舍五入
- 关于Delphi的四舍五入
- 关于Java的四舍五入
- ios中四舍五入的方法
- 关于四舍五入的问题,toFixed()
- 关于四舍五入的问题,toFixed()
- 关于四舍五入的问题,toFixed()
- 关于四舍五入的问题,toFixed()
- 关于c#的四舍五入问题
- 关于javascript小数的四舍五入
- 关于mysql的decimal四舍五入
- 关于小数四舍五入的问题
- C++ 关于四舍五入的问题
- C#关于中国式的四舍五入
- 关于BigDecimal数据的四舍五入
- ios四舍五入
- 一个字符转换及计算的思考题
- Java中获取键盘输入值的三种方法
- BOM设计的一些问题及解决方案探讨----合版BOM
- 还你一个积分墙的真相——手游那点事六问积分墙
- Sencha Touch学习笔记(四)数据
- IOS 关于四舍五入的
- DOM4j操作xml文件
- CentOS 搭建 LAMP环境
- hadoop端口使用配置总结(非常好的总结)
- 关于autocomplete的使用注意
- 电脑win7旗舰版开机错误post error occurs解决措施
- dllmain
- java string 字符串替换
- Jekyll本地环境搭建