Search code examples
iossqlitedoublensnumber

Why is my function outputting a random value, which looks like a memory address?


I'm trying to output the value of a latitude and longitude, which I have queried from an sql table. My issue is that when the value is 0.000000 it outputs something along the lines of 1.04480899798659e-276. I have checked the value of the output above these two line of code:

latitudeOutput.text = [[NSNumber numberWithDouble:[theGPS latitude]] stringValue];
longitudeOutput.text = [[NSNumber numberWithDouble:[theGPS longitude]] stringValue];

and it should definitely be outputting 0.000000 . Does anyone know why this is happening, and anyway I can fix it?

----Added Section----

Why does this if else statement carry straight on through to the else section, despite the latitude and longitude value being equal to 0.000000?

if(latitude == @"0.000000" && longitude == @"0.000000"){
    latitudeOutput.textColor = [UIColor grayColor];
    longitudeOutput.textColor = [UIColor grayColor];
    latitudeOutput.text = @"Latitude";
    longitudeOutput.text = @"Longitude";
}
else {
    latitudeOutput.text = latitude;
    longitudeOutput.text = longitude;
}

Solution

  • Memory addresses have format of 0xdeadbeef.

    Your value is a double written in a "variation" of scientfic notation. It is something extremely close to 0.0 (wolfram alpha visualization)

    This happens because of how computers store floating point values. You can read more here.

    To fix your issue, I'd suggest formating the output to include only as much digits after decimal point as you need.

    NSString* formattedNumber = [NSString stringWithFormat:@"%.02f", [theGPS latitude]];
    

    This will give you 2 places after decimal point.