0

Let my string is 77777788888 after converting to float its coming as 77777788928. But I want the accurate number

I have already tried with [str floatValue],[str doubleValue] and [str longlongValue]

Baidyanath
  • 167
  • 1
  • 4
  • 14
  • This might not be the case, but if you're using this to save for example a telephone number or something of the likes (something that doesn't need mathematical manipulations), you might be better of using a NSString instead. – tombrtls Nov 07 '12 at 09:59

4 Answers4

0

To float

float myFloat = [myString floatValue];

To Double

double myDouble = [myString doubleValue];
Future2020
  • 9,939
  • 1
  • 37
  • 51
0
NSLog(@"%.0f", [@"77777788888" floatValue]);
NSLog(@"%.0f", [@"77777788888" doubleValue]);
NSLog(@"%lld", [@"77777788888" longLongValue]);

Produces the output

77777788928
77777788888
77777788888

So if you are not getting good results, something else is wrong.

borrrden
  • 33,256
  • 8
  • 74
  • 109
0

maybe the problem is that your double is going out of range, check this other question

Community
  • 1
  • 1
tkanzakic
  • 5,499
  • 16
  • 34
  • 41
0

int is a 32 bit/4 byte value.

I am surprised are cannot comprehend (as of now), why you are getting that.

For now, you can use the following and this will work —

NSString *string = @"77777788888";
NSLog(@"%lld", [string longLongValue]);

I'll surely post a reply when I understand the issue.

Regards

p0lAris
  • 4,750
  • 8
  • 45
  • 80