0

I am trying to convert a string to a double value to use it as a coordinate in an annotation.

I need the double value to have 5 decimal places to be placed on the map.

Let's say the string is hello and the double I need is whatsup.

The format of the string is exactly the way I want it and I want to make it a double, so I use the doublevalue property of the string.

NSString *hello = @"12.61655";
double whatsup = hello.doubleValue;
NSLog(@"%f",whatsup); //this gives 12.616550 WITH A ZERO in the end
NSLog(@"%.5f",whatsup); //This gives me the correct value of 12.61655 with only 5 decimal places

So now if I want to write:

coordinate.latitude = whatsup 

it gives the double with the extra zero.

How can I write

coordinate.latitude = SOMETHING HERE 

which is the double with only 5 decimal places?
Can I implement the "%.5f" here somehow?

Have tried the numberformatter but it gives me an NSnumber. I need a double:

I am using this code in a for loop to plot the annotations (pins).

When i use the same forloop with doubles I hardcode it works fine. But when I use this code to get the values from the csv file, I dont get any pins:

double latitudeFromCSV = [[components objectAtIndex:0] doubleValue];
double longitudeFromCSV = [[components objectAtIndex:1] doubleValue];
CLLocationCoordinate2D annotationCoord;
annotationCoord.latitude = latitudeFromCSV;
annotationCoord.longitude = longitudeFromCSV;
A-Live
  • 8,904
  • 2
  • 39
  • 74
B-Man
  • 2,209
  • 3
  • 22
  • 35
  • `NSLog(@"coordinate.latitude = %.5f", whatsup);` – Till Sep 11 '12 at 00:08
  • Oh wait, you don't mean you want to store the double value w/o the leading zeros, do you ? – A-Live Sep 11 '12 at 00:16
  • Yeah i want to store the double value without the zero at the end... store it with only 5 decimals. is that possible? – B-Man Sep 11 '12 at 00:19
  • No, it's not possible. A `double` is a floating-point value which has no inherent external "format" -- you determine how it displays (how many digits, etc) when you print it, not when you set the value. – Hot Licks Sep 11 '12 at 00:38
  • I'd suggest you NSLog the values you're parsing from the CSV file and see if they're what you expect. – Hot Licks Sep 11 '12 at 00:39
  • in the nslog i use the .5f and it shows fine. but i think you are right and the error must be elsewhere. – B-Man Sep 11 '12 at 00:46

1 Answers1

1

Check out the NSString method -stringWithFormat:

But to make it good you'll need to remove the leading zeros as you can't know there will be no value like @"12.50000".


NSNumberFormatter * nf = [[NSNumberFormatter new] autorelease];
nf.maximumFractionDigits = 5;
NSLog(@"%@", [nf stringFromNumber:[NSNumber numberWithFloat:@"12.61655f".doubleValue]]);

should give you 12.61655 as an output string.

A-Live
  • 8,904
  • 2
  • 39
  • 74
  • I cant use a string. needs to be double to be accepted as a coordiante. – B-Man Sep 11 '12 at 00:20
  • I need you to understand 12.61655f and 12.61655000000f are the same double values. The only thing that makes difference is that you might want to remove the leading zeros while using it as string. I'll add some NSNumber magic for you. – A-Live Sep 11 '12 at 00:22
  • the problem is that when i pass the double as a latitude it contains the zero in the end (6 decimal places) instead of 5 and the coordinate fails. so i need to pass a double with 5 decimals. – B-Man Sep 11 '12 at 00:25
  • What do you mean by `coordinate fails`, are you using it as URL parameter or something ? because the memory representation of this two writings is the same. – A-Live Sep 11 '12 at 00:26
  • I have a CSV file with 3 components, lat, long and name. I am using this code in a for loop to plot the annotations (pins). when i use the same forloop with doubles i hardcode it works fine. but when i use this code to get the values from the csv file, i dont get any pins: double latitudeFromCSV = [[components objectAtIndex:0] doubleValue] ; double longitudeFromCSV = [[components objectAtIndex:1] doubleValue]; CLLocationCoordinate2D annotationCoord; annotationCoord.latitude = latitudeFromCSV; annotationCoord.longitude = longitudeFromCSV; – B-Man Sep 11 '12 at 00:27
  • So you are writing a file ? Create the string either with help of `stringWithFormat:@"%.5f"` or `NSNumberFormatter` and write a string there. – A-Live Sep 11 '12 at 00:28
  • i want to assign some coordinates with data from a csv file (comma seperated value) excel-ish. when i assign: latitude = [components objectAtIndex:0] i assign a string and that is not good, it needs a double latitude = [[components objectAtIndex:0] doubleValue] is returning an extra zero to the latitude making the pin UNplottable. does that make sense? – B-Man Sep 11 '12 at 00:30
  • Sorry, it doesn't because 12.61655f and 12.616550f are the same values represented in the same way at memory. Have you tried to hardcode exactly 12.616550f ? – A-Live Sep 11 '12 at 00:33
  • I cant because there are 150 rows in the csv. so i am running a loop to iterate them it would take ages to hardcode them. i just dont understand why the pins are showing up when i hardcode the latitude and longitude, and that when i replace with double from the csv file they dont pin on the map. and i am validating the doubles with a nslog, so they exist and should be replacing the latitude and longitude in the for loop. are you good with annotations? By the way thanks for your time. what would amateur programmers do without you guys help? :) – B-Man Sep 11 '12 at 00:37
  • You don't need all annotations to be different to understand if it's displayed, so make it all hardcoded 12.616550f in the loop. Might be also a good idea to check the second coordinate. – A-Live Sep 11 '12 at 00:40
  • it is a list of 150 shops. and they all have different latitudes and longitudes given in the csv file. so i need to iterate through the array made from the csv file and add latitude and longitude for each row in a new annotation. – B-Man Sep 11 '12 at 00:42
  • Yes, to test the hardcoded coordinates you can do the same thing but not to read the coordinates from CSV, assign the same hardcoded value for every shop. You will get 150 annotations all in the same place, that's OK. – A-Live Sep 11 '12 at 00:43
  • how can i nslog the number of annotations i have when they are on top of each other? for all i know there could only be one? – B-Man Sep 11 '12 at 00:47
  • i just did it , and got all 150 annotations in the hardcoded spot. so the issue is the in the double parsed from the csv file. driving med mad hehe – B-Man Sep 11 '12 at 00:55
  • That might really be the coordinates problem like coordinates over the maximum value. You do have the second coordinate. If you are sure some annotation is not displayed, please post it's both coordinates as written at CSV. There also might be a situation with two shops having the same coordinates so you'll see only one annotation. – A-Live Sep 11 '12 at 00:56
  • I THINK i finally got it to work... The data in the csv was "long,lat", and i thought it was the opposite "lat, long". So when i sat the coordinates, i switched the long and the lat and the pins were outside this world. So now i swtiched them and they look great!!! :) – B-Man Sep 11 '12 at 01:03
  • THANKS all for your help. I did think the double is the same no matter how many zeroes it had, but everything else made no sense. so my noobidity stood in the way and the answer is almost always in the simplest of thingS! :) thanks once again. – B-Man Sep 11 '12 at 01:04