• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Objective-C中整数与字符串的相互转换

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

2013/4/15整理:

 

将整数转换成字符串 Convert Integer to NSString:
方法一:
int Value = 112233;
NSString *ValueString = [NSString stringWithFormat:@"%d", Value];
方法二:
[[NSNumber numberWithInt: 123] stringValue];
 
得到C风格的字符串 C String
char *ValueasCString = (char *)[ValueString UTF8String];
 
将字符串转换成整数或浮点数
Convert String to Integer or float
NSString has some useful methods:
-(int) intValue
-(float) floatValue
NSString *aNumberString = @"123";
int i = [aNumberString intValue];
 
字符串转换为日期
NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];//实例化一个NSDateFormatter对象
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//设定时间格式,这里可以设置成自己需要的格式
NSDate *date =[dateFormat dateFromString:@"1980-01-01 00:00:01"];
 
日期转换为字符串
NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];//实例化一个NSDateFormatter对象
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//设定时间格式,这里可以设置成自己需要的格式
NSString *currentDateStr = [dateFormat stringFromDate:[NSDate date]];

 

使用NSNumberFormatter将数值转换成字符串
Simplifying NSNumberFormatter Use on the iPhone With Objective-C Categories
Here are the different formatter styles, and the result when converting [NSNumber numberWithDouble:123.4] into an NSString.
NSNumberFormatterNoStyle (123)
NSNumberFormatterDecimalStyle (123.4)
NSNumberFormatterCurrencyStyle ($123.40)
NSNumberFormatterPercentStyle (12,340%)
NSNumberFormatterScientificStyle (1.234E2)
NSNumberFormatterSpellOutStyle (one hundred and twenty-three point four)
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterCurrencyStyle;
NSString *string = [formatter stringFromNumber:number];
[formatter release];

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Objective-C的泛型发布时间:2022-07-12
下一篇:
[Objective-C]Block实现回调和简单的学习思考发布时间:2022-07-12
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap