John Smith
John Smith

Reputation: 1172

Losing accuracy with NSString floatValue?

I'm a beginner in Objective-c and i have a little issue that i'm sure you will resolve in two seconds :P

I searched for casting a NSString to a float value on StackOverflow and found that [NSString floatValue] can help me i tried but i don't know why i'm losing all the value after the dot ...

here the code of my issue:

- (void)setTimerDetails:(NSString *)time {
    CCLOG(@"TEST1 = %@", time);
    CCLOG(@"TEST2 = %f", [time floatValue]);
    CCLOG(@"TEST3 = %f", ([time floatValue] / 60));
    self.test.progress = ([time floatValue] / 60);
    self.crono.text = time;
}

as you can see it's for use in a progress bar :P

But here are the logs:

2012-05-14 10:53:39.279 Colors[533:1be03] TEST1 = 58,733
2012-05-14 10:53:39.280 Colors[533:1be03] TEST2 = 58.00000
2012-05-14 10:53:39.280 Colors[533:1be03] TEST3 = 0.966667

why is there only 0 after the dot for the TEST2 line ? :s

Thank you for helping me guys :)

Upvotes: 0

Views: 253

Answers (1)

adali
adali

Reputation: 5977

because

Colors[533:1be03] TEST1 = 58,733

it's 58,773 not 58.773

so when it turn to floatValue, it only grab the "58" part

Upvotes: 3

Related Questions