Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-21-2010, 09:44 PM   #1 (permalink)
Code before baby wakes up
 
RobotWoods's Avatar
 
Join Date: Sep 2009
Posts: 189
RobotWoods is on a distinguished road
Default String to Double turns into ZERO

Not sure what I'm missing here, so thanks for any help.
I am parsing some XML into a variety of strings. A few of them are digit strings (latitude or longitude), that I need to use as doubles. But when I convert them, they turn into zero.

PHP Code:
double newDouble = [someStrings.number doubleValue];
NSLog(@"string: %@"someStrings.number);
NSLog(@"double: %d"newDouble); 
The string log entry unsurprisingly is correct, and newDouble is 0.

My one thought is that is has something to do with something done during the initial storing of the data that is preventing conversion.

The specific line for that is:

PHP Code:
[someStrings setValue:currentElementValue forKey:elementName]; 
I have also tried making an intermediary string with stringWithFormat, and then converting that, but no joy.

Thanks

Last edited by RobotWoods; 06-21-2010 at 10:12 PM.
RobotWoods is offline   Reply With Quote
Old 06-22-2010, 04:40 AM   #2 (permalink)
A Single-Serving Friend
 
Join Date: Mar 2010
Location: Groningen, NL
Posts: 491
Robert Paulson is on a distinguished road
Default

Hi,

have you tried using floatValue instead of doubleValue? I don't know why but I had some trouble using doubles as well the other day that I didn't have using float. It's a rather random guess but maybe it helps.

Cheers,
Bob
__________________
We are God’s middle children, according to Tyler Durden, with no special place in history and no special attention.

Consider saying thanks by buying my app. :]
Robert Paulson is offline   Reply With Quote
Old 06-22-2010, 06:37 AM   #3 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,549
RLScott is on a distinguished road
Default

QUOTE=RobotWoods;213248]
NSLog(@"double: %d", newDouble);[/php]
[/quote]
The format specifier "%d" is for integers, not for doubles. Doubles are specified by "%f".

Last edited by RLScott; 06-22-2010 at 08:18 AM. Reason: Corrected "%lf" to "%f"
RLScott is offline   Reply With Quote
Old 06-22-2010, 07:28 AM   #4 (permalink)
Registered Member
 
Join Date: Jun 2010
Location: Sevilla
Posts: 39
juanpecm is on a distinguished road
Default

Quote:
Originally Posted by RobotWoods View Post
Not sure what I'm missing here, so thanks for any help.
I am parsing some XML into a variety of strings. A few of them are digit strings (latitude or longitude), that I need to use as doubles. But when I convert them, they turn into zero.

PHP Code:
double newDouble = [someStrings.number doubleValue];
NSLog(@"string: %@"someStrings.number);
NSLog(@"double: %d"newDouble); 
The string log entry unsurprisingly is correct, and newDouble is 0.

My one thought is that is has something to do with something done during the initial storing of the data that is preventing conversion.

The specific line for that is:

PHP Code:
[someStrings setValue:currentElementValue forKey:elementName]; 
I have also tried making an intermediary string with stringWithFormat, and then converting that, but no joy.

Thanks
Try this

PHP Code:
double newDouble = [someStrings doubleValue];
NSLog(@"string: %@"someStrings);
NSLog(@"double: %d"newDouble); 
juanpecm is offline   Reply With Quote
Old 06-22-2010, 08:16 AM   #5 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,549
RLScott is on a distinguished road
Default

Quote:
Originally Posted by RLScott View Post
The format specifier "%d" is for integers, not for doubles. Doubles are specified by "%lf".
My mistake. The correct specifier for doubles is just "%f". But I was right that "%d" is for 32-bit integers. So you cannot trust it to correctly render a double.

Robert Scott
Ypsilanti, Michigan
RLScott is offline   Reply With Quote
Old 06-22-2010, 09:03 AM   #6 (permalink)
Code before baby wakes up
 
RobotWoods's Avatar
 
Join Date: Sep 2009
Posts: 189
RobotWoods is on a distinguished road
Default

Robert, thanks for the suggestion, I have tried floatValue, as well as using CGFloat as the type for the new item. I have also tried Scanner and NumberFormatter to no avail, I am jack's seething frustration.

Juan, the issue is that "someStrings" has multiple attributes/elements, so in actuality there is a someStrings.latitude and someStrings.longitude

And RLScott, thanks for pointing that out, but regardless of my NSLog syntax, when I try to USE these numbers (as lat/long of a pin annotation) it's always landing at (0,0).

I found that if floatValue / doubleValue are trying to convert an "invalid number" they just return zero, so I think that is the issue, but just what about these numbers is invalid is escaping me...the XML file that I'm pulling from is generated via PHP, so I don't know if there is some encoding detail I need to change/convert.

Thank you all for your suggestions, I am away from my mac for the time being, but will bang my head against it again tonight.
RobotWoods is offline   Reply With Quote
Old 06-22-2010, 09:20 PM   #7 (permalink)
Code before baby wakes up
 
RobotWoods's Avatar
 
Join Date: Sep 2009
Posts: 189
RobotWoods is on a distinguished road
Default

Got it...it was a ridiculous detail in my php that was outputting the xml...I had

PHP Code:
echo "<lat>".$row["latitude"]."</lat>\n";
echo 
"<lng>".$row["longitude"]."</lng>\n"
with the understanding that NSXMLParser would only grab the string between the start and end of the elements, but somehow the newline was getting included, making the string of digits an invalid number...

PHP Code:
echo "<lat>".$row["latitude"]."</lat>";
echo 
"<lng>".$row["longitude"]."</lng>"
Works just fine. Thanks for your suggestions.
RobotWoods is offline   Reply With Quote
Reply

Bookmarks

Tags
doublevalue, nsstring, xml

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 333
8 members and 325 guests
anothermine, Chickenrig, Domele, givensur, heshiming, michaelhansen, PixelInteractive, Sloshmonster
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,892
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:18 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0