object-c中NSString与int和float的相互转换
2015-10-13 21:43
489 查看
NSString *tempA = @"123";
NSString *tempB = @"456";
1,字符串拼接
NSString *newString = [NSString stringWithFormat:@"%@%@",tempA,tempB];
2,字符转int
int intString = [newString intValue];
3,int转字符
NSString *stringInt = [NSString stringWithFormat:@"%d",intString];
4,字符转float
float floatString = [newString floatValue];
5,float转字符
NSString *stringFloat = [NSString stringWithFormat:@"%f",intString];
四舍五入问题
-(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 *sb = [self notRounding:s afterPoint:2];
NSLog(@"sb = %@",sb);
输出结果为:sb = 0.12
接下来介绍NSDecimalNumberHandler初始化时的关键参数:decimalNumberHandlerWithRoundingMode:NSRoundDown,
NSRoundDown代表的就是 只舍不入。
scale的参数position代表保留小数点后几位。
NSString *tempB = @"456";
1,字符串拼接
NSString *newString = [NSString stringWithFormat:@"%@%@",tempA,tempB];
2,字符转int
int intString = [newString intValue];
3,int转字符
NSString *stringInt = [NSString stringWithFormat:@"%d",intString];
4,字符转float
float floatString = [newString floatValue];
5,float转字符
NSString *stringFloat = [NSString stringWithFormat:@"%f",intString];
四舍五入问题
-(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 *sb = [self notRounding:s afterPoint:2];
NSLog(@"sb = %@",sb);
输出结果为:sb = 0.12
接下来介绍NSDecimalNumberHandler初始化时的关键参数:decimalNumberHandlerWithRoundingMode:NSRoundDown,
NSRoundDown代表的就是 只舍不入。
scale的参数position代表保留小数点后几位。
相关文章推荐
- OC的冒泡排序
- Objective-C内存管理详解——dealloc方法
- WaitForSingleObject的用法
- 黑马程序员-----------Objective-c基础-----------继承和多态
- Objective-C中代码块的学习
- Objective-c开发中混合使用ARC
- objective-c常用数学方法
- mutable method is send to imutable object
- Objective-C内存管理详解——对象所有权
- Objective-C内存管理详解——引用计数
- Object-C 关键字之property,assign,copy,retain,release
- Object-C数据类型
- 详解Objective-C的优点和不足
- 接口测试之JsonObject
- objective-c制作QQ登录界面
- Object C Dictionary字典
- Object-C宏定义与const变量
- java.lang.UnsupportedClassVersionError: org/json/JSONObject : Unsupported major.minor version 52.0
- java.lang.UnsupportedClassVersionError: org/json/JSONObject : Unsupported major.minor version 52.0
- Foundation Kit框架概叙