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 01-12-2012, 04:39 PM   #1 (permalink)
Registered Member
 
Join Date: Jan 2012
Location: Bay St. Louis, MS
Posts: 2
tallybear is on a distinguished road
Default Issue converting string to double

Here's what I'm doing...

I have some XML that I'm parsing out. One of these elements is a rate for a campground. The parsed string (just an example) comes out to something like 24.0000. It always has 4 decimal places. I'm trying to convert this to a double and truncate the last 2 decimal places. I've tried several different things, but none of them have worked. Does anyone have any experience with this?
tallybear is offline   Reply With Quote
Old 01-12-2012, 06:59 PM   #2 (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 tallybear View Post
Here's what I'm doing...

I have some XML that I'm parsing out. One of these elements is a rate for a campground. The parsed string (just an example) comes out to something like 24.0000. It always has 4 decimal places. I'm trying to convert this to a double and truncate the last 2 decimal places. I've tried several different things, but none of them have worked. Does anyone have any experience with this?
Truncating the last two decimal places is something that is applicable to strings that are made from doubles, not doubles that are made from strings. A double has no particular number of decimal places.

Since you already have the string form to begun with, you could just truncate the string first by making another string that leaves off the last two characters. Then convert that string to a double, is that what you mean?
RLScott is offline   Reply With Quote
Old 01-13-2012, 10:06 AM   #3 (permalink)
Registered Member
 
Join Date: Jan 2012
Location: Bay St. Louis, MS
Posts: 2
tallybear is on a distinguished road
Default

Quote:
Originally Posted by RLScott View Post
Truncating the last two decimal places is something that is applicable to strings that are made from doubles, not doubles that are made from strings. A double has no particular number of decimal places.

Since you already have the string form to begun with, you could just truncate the string first by making another string that leaves off the last two characters. Then convert that string to a double, is that what you mean?
I found what I was looking for:

double num1 = [string doubleValue];
NSString *parsedText = [[NSString alloc] initWithFormat:@"%0.2f", num1];

Thanks anyway.
tallybear is offline   Reply With Quote
Old 01-13-2012, 05:17 PM   #4 (permalink)
Registered Member
 
Join Date: Dec 2010
Location: Seattle, WA
Posts: 408
RickSDK is on a distinguished road
Default

by the way, you can avoid the alloc by using StringWithFormat:

NSString *parsedText = [NSString StringWithFormat:@"%0.2f", num1];
__________________
Check out my apps

RickSDK is offline   Reply With Quote
Old 01-13-2012, 06:15 PM   #5 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by RickSDK View Post
by the way, you can avoid the alloc by using StringWithFormat:

NSString *parsedText = [NSString StringWithFormat:@"%0.2f", num1];
To say you can "avoid the alloc" is a little misleading.

stringWithFormat is a "convenience method." It creates and returns an autoreleased string object. It would be more accurate to say that you can avoid having to release the string after using it.

Internally, the implementation of stringWithFormat probably looks something like this:

Code:
+ (id)stringWithFormat:(NSString *)format, ...
{
  result = [NSString alloc] initWithFormat: format, ...];
  return [result autorelease];
}
(It won't be exactly like that, because there's some magic I don't remember that you have to do to pass the variable number of arguments on to initWithFormat, but you get the idea.)

The OPs code, using initWithFormat, had a memory leak unless he was using ARC.

In manual reference counting, after you alloc/init and object, you have to release it when you are done with it.

stringWithFormat returns an autoreleased string, so you don't have to release it.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.

Last edited by Duncan C; 01-14-2012 at 07:31 AM.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Tags
doubles, ios, iphone, objective-c, strings

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: 379
11 members and 368 guests
7twenty7, Atatator, glenn_sayers, guusleijsten, iphonedevshani, j.b.rajesh@gmail.com, QuantumDoja, sacha1996, Sami Gh, tim0504, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,674
Threads: 94,122
Posts: 402,907
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Atatator
Powered by vBadvanced CMPS v3.1.0

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