2013-04-15 34 views
-2

我试图格式化该字符串,秒后跟3个小数点,例如1.123。
我该如何去做这件事?格式cctime,小数点后3位

-(void)update:(ccTime)dt { 
totalTime += dt*1000; 
currentTime = (int)totalTime; 
if (myTime < currentTime) 
{ 
    myTime = currentTime; 
    [timeLabel setString:[NSString stringWithFormat:@"%i", myTime]]; 
} 

}

+0

请书签[IEEE规范的printf(http://pubs.opengroup.org/onlinepubs/009695399/functions/fprintf.html) 。任何使用'stringWithFormat:'的人都应该熟悉那个页面。 – rmaddy

+0

我不是。伟大的联系,我一直在想,如果这样的网页存在,但从来没有打扰检查。 – LearnCocos2D

回答

3

您可以使用

[NSString stringWithFormat:@"%.3f", someFloatOrDouble]; 
+0

如果这是为了显示的目的,还有:使用一个固定宽度的字体和一个格式说明符,它将产生一个固定的字符串长度,否则,如果你每16毫秒更新一次,你的游戏者可能会晕船(希望你会跑近到每秒60帧)。 – YvesLeBorg

+0

实际上,为了显示,应该使用'NSNumberFormatter',而不是'stringWithFormat'来获取对用户的语言环境来说看起来正确的十进制数字。 – rmaddy

相关问题