格式化浮点值以获取小数点前的数字 [英] Format a float value to get the digits before a decimal point
本文介绍了格式化浮点值以获取小数点前的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我会很感激,如果你能告诉我如何能让标签显示
0:14而不是14.765。
另外,如果能告诉我如何显示0: 04如果进度是4秒钟的话。
解决方案
float time = 14.765;
int mins = time / 60;
int secs = time-(mins * 60);
NSString * display = [NSString stringWithFormat:@%d:%02d,mins,secs];
结果:
14.765 => 0:14
30.000 => 0:30
59.765 => 0:59
105.999 => 1:45
编辑
此外,单行:
浮动时间= 14.765;
NSString * display = [NSString stringWithFormat:@%d:%02d,(int)time / 60,(int)time%60];
In my application I have a music player, which plays music with a length of 0:30 seconds.
However in a UILabel I am currently displaying the progress, and as it is a float, the label is displaying i.e 14.765.
I would appreciate it if you could tell me how I could get the label to display
0:14 rather than 14.765.
Also, I would appreciate it if you could tell me how I could display 0:04 if the progress was 4seconds in.
解决方案
This works properly:
float time = 14.765;
int mins = time/60;
int secs = time-(mins*60);
NSString * display = [NSString stringWithFormat:@"%d:%02d",mins,secs];
Results:
14.765 => 0:14
30.000 => 0:30
59.765 => 0:59
105.999 => 1:45
EDIT
In addition the 'one liner':
float time = 14.765;
NSString * display = [NSString stringWithFormat:@"%d:%02d",(int)time/60,(int)time%60];
这篇关于格式化浮点值以获取小数点前的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文