我有以下代码意味着将毫秒转换为小时,分钟和秒:
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 style06001
where the number of digits after the hexadecimal-point character is equal to the precision specification.