Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C truncate double value to 2 decimal places

I'm trying to truncate a double value to two decimal places. I tried using:

[highScoreLabel setText:[NSString stringWithFormat:@"Highscore: %.2f",finalTime]];

but I found that it rounds the number after 2 places. This makes the score I'm reporting to game center and the score displayed in app different. How can I cut off the rest of the decimal without rounding it?

like image 982
SomeGuy Avatar asked Oct 28 '25 16:10

SomeGuy


1 Answers

@dasdom answer is great. But if you do not like implicit casting you can use

finalTime = trunc(finalTime * 100) / 100;

If you will receive an error that 'trunc' function is unknown - just import math.h header:

#import <math.h>
like image 90
Avt Avatar answered Oct 31 '25 05:10

Avt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!