IOS 保留小数点后几位
2014-03-25 10:40
375 查看
/******高级方法 ********/
如何只舍不入。比如 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代表保留小数点后几位。
介绍一下参数:
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代表保留小数点后几位。
/****** c 方法 *********/
1,四舍五入法
float numberToRound; int result; numberToRound = 5.61; result = (int)roundf(numberToRound); NSLog(@"roundf(%.2f) = %d", numberToRound, result); //输出 roundf(5.61) = 6
numberToRound = 5.41; result = (int)roundf(numberToRound); NSLog(@"roundf(%.2f) = %d", numberToRound, result); //输出 roundf(5.41) = 5
2、进位方法
float numberToRound; int result; numberToRound = 5.61; result = (int)ceilf(numberToRound); NSLog(@"ceilf(%.2f) = %d", numberToRound, result); //输出 ceilf(5.61) = 6
numberToRound = 5.41; result = (int)ceilf(numberToRound); NSLog(@"ceilf(%.2f) = %d", numberToRound, result); //输出 ceilf(5.41) = 6
3、摸位方法
float numberToRound; int result; numberToRound = 5.61; result = (int)floorf(numberToRound); NSLog(@"floorf(%.2f) = %d", numberToRound, result); //输出 floorf(5.61) = 5
numberToRound = 5.41; result = (int)floorf(numberToRound); NSLog(@"floorf(%.2f) = %d", numberToRound, result); //输出 floorf(5.41) = 5
转载自:http://www.cnblogs.com/yingkong1987/archive/2012/12/18/2823077.html
如何只舍不入。比如 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代表保留小数点后几位。
介绍一下参数:
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代表保留小数点后几位。
/****** c 方法 *********/
1,四舍五入法
float numberToRound; int result; numberToRound = 5.61; result = (int)roundf(numberToRound); NSLog(@"roundf(%.2f) = %d", numberToRound, result); //输出 roundf(5.61) = 6
numberToRound = 5.41; result = (int)roundf(numberToRound); NSLog(@"roundf(%.2f) = %d", numberToRound, result); //输出 roundf(5.41) = 5
2、进位方法
float numberToRound; int result; numberToRound = 5.61; result = (int)ceilf(numberToRound); NSLog(@"ceilf(%.2f) = %d", numberToRound, result); //输出 ceilf(5.61) = 6
numberToRound = 5.41; result = (int)ceilf(numberToRound); NSLog(@"ceilf(%.2f) = %d", numberToRound, result); //输出 ceilf(5.41) = 6
3、摸位方法
float numberToRound; int result; numberToRound = 5.61; result = (int)floorf(numberToRound); NSLog(@"floorf(%.2f) = %d", numberToRound, result); //输出 floorf(5.61) = 5
numberToRound = 5.41; result = (int)floorf(numberToRound); NSLog(@"floorf(%.2f) = %d", numberToRound, result); //输出 floorf(5.41) = 5
转载自:http://www.cnblogs.com/yingkong1987/archive/2012/12/18/2823077.html
相关文章推荐
- IOS 基础开发之 保留小数点后几位
- iOS 保留到小数点后几位
- iOS数字显示保留几位小数点
- java处理保留小数点后几位
- js保留小数点后几位的写法
- php number_format()保留小数点后几位有效数的函数 千位分组来格式化数字
- java保留小数点后几位
- C#保留小数点几位
- java处理保留小数点后几位
- iOS小数点格式化:如果有两位小数不为0则保留两位小数,如果有一位小数不为0则保留一位小数,否则显示整数
- 关于精确度-保留小数点后几位小数
- 四舍五入强制保留小数点后几位数字
- java处理保留小数点后几位
- JAVA-保留小数点后几位
- iOS NSDecimalNumber精确数值计算以及小数点后精确保留2位数字
- JSP页面EL表达式double保留小数点后几位
- java保留小数点后几位,不足的用0补
- ios NSString format 保留小数点 float double
- ios NSString format 保留小数点 float double
- iOS 判断输入的字符串小数点前几位和后几位方法(适用于金额输入)