加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Objective C将int转换为NSString(iPhone)

发布时间:2020-12-16 07:53:00 所属栏目:百科 来源:网络整理
导读:我有以下代码意味着将毫秒转换为小时,分钟和秒: int hours = floor(rawtime / 3600000);int mins = floor((rawtime % 3600000) / (1000 * 60));int secs = floor(((rawtime % 3600000) % (1000 * 60)) / 1000);NSLog(@"%d:%d:%d",hours,mins,secs);NSString
我有以下代码意味着将毫秒转换为小时,分钟和秒:
int hours = floor(rawtime / 3600000);

int mins = floor((rawtime % 3600000) / (1000 * 60));
int secs = floor(((rawtime % 3600000) % (1000 * 60)) / 1000);

NSLog(@"%d:%d:%d",hours,mins,secs);

NSString *hoursStr = [NSString stringWithFormat:@"%d",hours];
NSString *minsStr = [NSString stringWithFormat:@"%d",mins];
NSString *secsStr = [NSString stringWithFormat:@"%d",secs];

NSLog(@"%a:%a:%a",hoursStr,minsStr,secsStr);

非常坦率的. Rawtime是一个int值,值为1200.输出如下:

0:0:1

0x1.3eaf003d9573p-962:0x1.7bd2003d3ebp-981:-0x1.26197p-698

为什么将ints转换为字符串会产生这样的野数?我试过使用%i和%u,他们没有任何区别.发生什么事?

解决方法

您必须使用%@作为NSString的转换说明符.将最后一行更改为:
NSLog(@"%@:%@:%@",secsStr);

%a意味着完全不同的东西.从printf()man page:

aA
The double argument is rounded and converted to hexadecimal notation in the style

06001

where the number of digits after the hexadecimal-point character is equal to the precision specification.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读