Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 20, 2010, 02:59 AM   #1
kaydell.leavitt
macrumors regular
 
Join Date: Apr 2010
How Do I Convert from an NSString to a double?

Hi,

I need to learn how to convert from an NSString to a double.

Thanks for helping me.

-- Kaydell

Code:
- (double) getCost {
	NSString *string = @"2.0";
	NSScanner *scanner = [NSScanner scannerWithString: string];
	double *cost = [scanner scanDouble];  <<< this gives me the warning: that 'NSScanner' may not respond to -scanDouble
	return *cost;
}

Last edited by kainjow; Apr 20, 2010 at 09:56 AM. Reason: code tags
kaydell.leavitt is offline   0 Reply With Quote
Old Apr 20, 2010, 03:05 AM   #2
zed2
macrumors 6502a
 
Join Date: Jul 2004
Quote:
Originally Posted by kaydell.leavitt View Post
Hi,

I need to learn how to convert from an NSString to a double.

Thanks for helping me.

-- Kaydell

- (double) getCost {
NSString *string = @"2.0";
NSScanner *scanner = [NSScanner scannerWithString: string];
double *cost = [scanner scanDouble]; <<< this gives me the warning: that 'NSScanner' may not respond to -scanDouble
return *cost;
}


double *cost = [string doubleValue];
__________________
http://www.rho.cc
zed2 is offline   0 Reply With Quote
Old Apr 20, 2010, 03:23 AM   #3
kaydell.leavitt
Thread Starter
macrumors regular
 
Join Date: Apr 2010
Thanks, But I Still Need More Help

Thanks for your help, but I still need more help.

Code:
- (double) getCost {
	NSString *string = @"2.0";
	double *cost = [string scanDouble]; <<< error: incompatible types..
	return *cost;
}

Last edited by kainjow; Apr 20, 2010 at 09:56 AM. Reason: code tags
kaydell.leavitt is offline   0 Reply With Quote
Old Apr 20, 2010, 03:27 AM   #4
zed2
macrumors 6502a
 
Join Date: Jul 2004
Quote:
Originally Posted by kaydell.leavitt View Post
Thanks for your help, but I still need more help.

- (double) getCost {
NSString *string = @"2.0";
double *cost = [string scanDouble]; <<< error: incompatible types..
return *cost;
}
Code:
- (double) getCost {
NSString *string = @"2.0";
double *cost = 0.0;
if ([string scanDouble]) {
   cost = [string doubleValue];
}
return *cost;
}
__________________
http://www.rho.cc

Last edited by kainjow; Apr 20, 2010 at 09:56 AM. Reason: code tags
zed2 is offline   0 Reply With Quote
Old Apr 20, 2010, 03:38 AM   #5
kaydell.leavitt
Thread Starter
macrumors regular
 
Join Date: Apr 2010
Thank You for Your Help

Thanks for your help, again.

Here's what's working for me now.

Code:
- (double) getCost {
	NSString *string = @"2.0";
	double *cost;
	if ([string doubleValue]) {
		*cost = [string doubleValue];
	} else {
		*cost = 0.0;
	}
	return *cost;
}
-- Kaydell

Last edited by kainjow; Apr 20, 2010 at 09:56 AM. Reason: code tags
kaydell.leavitt is offline   0 Reply With Quote
Old Apr 20, 2010, 05:45 AM   #6
Guiyon
macrumors 6502a
 
Join Date: Mar 2008
Location: North Shore, MA
That is going to cause a crash in the worst case or a garbage value in the best as you are creating a pointer but the pointer goes nowhere!

Code:
double *cost;
When you dereference the above pointer and write to it you are going to be writing to some arbitrary section of memory. The double should NOT be a pointer because doubleValue does not return a pointer.

For example, using an NSString:
Code:
NSString *aString = @"0.234";
double cost = [aString doubleValue];
Or, using an NSScanner approach before:
Code:
NSScanner *scanner = [NSScanner scannerWithString:@"0.234"];
double cost;
[scanner scanDouble: &cost];
__________________
Make life easier if you have a programming question!
http://www.sscce.org/
Guiyon is offline   0 Reply With Quote
Old Apr 20, 2010, 10:21 AM   #7
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
And if you're going to use NSScanner, make sure you check scanDouble's return value and handle it if it's not successful.
kainjow is offline   0 Reply With Quote
Old Apr 20, 2010, 10:54 AM   #8
bredell
macrumors regular
 
Join Date: Mar 2008
Location: Uppsala, Sweden
Send a message via Skype™ to bredell
Also, note that the NSString method doesn't handle localized values making your program work only with numbers using the english format. To handle localized values you can use the NSScanner class, provided that you initialize it with the localizedScannerWithString method.
bredell is offline   0 Reply With Quote
Old Apr 20, 2010, 11:37 AM   #9
kaydell.leavitt
Thread Starter
macrumors regular
 
Join Date: Apr 2010
Quote:
Originally Posted by Guiyon View Post
That is going to cause a crash in the worst case or a garbage value in the best as you are creating a pointer but the pointer goes nowhere!

Code:
double *cost;
When you dereference the above pointer and write to it you are going to be writing to some arbitrary section of memory. The double should NOT be a pointer because doubleValue does not return a pointer.

For example, using an NSString:
Code:
NSString *aString = @"0.234";
double cost = [aString doubleValue];
Or, using an NSScanner approach before:
Code:
NSScanner *scanner = [NSScanner scannerWithString:@"0.234"];
double cost;
[scanner scanDouble: &cost];
OK. You're right. What I had compiled, but didn't execute

Here's what I believe really works:

Code:
- (double) getCost {
	NSString *string = [costDisplay text];
	double cost;
	if ([string doubleValue]) {
		cost = [string doubleValue];
	} else {
		cost = 0.0;
	}
	return cost;
}
kaydell.leavitt is offline   0 Reply With Quote
Old Apr 20, 2010, 11:42 AM   #10
kaydell.leavitt
Thread Starter
macrumors regular
 
Join Date: Apr 2010
Quote:
Also, note that the NSString method doesn't handle localized values making your program work only with numbers using the english format. To handle localized values you can use the NSScanner class, provided that you initialize it with the localizedScannerWithString method.
This reminds me. When I convert from double to NSString, I'm not getting any commas, as thousands separators.

I use a format specifier such as:

Code:
NSString *string = [NSString stringWithFormat: @"%10.2f", cost];
The above line of code works OK, except that there are no thousands separator.

How can I convert from a double to an NSString with thousands separators and always have two decimal places for US cents?

-- Kaydell
kaydell.leavitt is offline   0 Reply With Quote
Old Apr 20, 2010, 07:43 PM   #11
bredell
macrumors regular
 
Join Date: Mar 2008
Location: Uppsala, Sweden
Send a message via Skype™ to bredell
Quote:
Originally Posted by kaydell.leavitt View Post
How can I convert from a double to an NSString with thousands separators and always have two decimal places for US cents?
Normally you should be able to use the format string "%'10.2f" but it doesn't seem to be supported by the iPhone.

You have to use the NSNumberFormatter class or, if you want to hardcode the format, you could do some string operations to insert commas at the right places in the string.
bredell is offline   0 Reply With Quote
Old Apr 23, 2010, 05:15 AM   #12
kaydell.leavitt
Thread Starter
macrumors regular
 
Join Date: Apr 2010
Quote:
Normally you should be able to use the format string "%'10.2f" but it doesn't seem to be supported by the iPhone.
I did get the format string to work and it is OK, except it's not localizable to other locales.

Code:
// This works on the iPhone OS, but it isn't localizable
NSString* doubleToCurrencyString1(double d) {
	NSString *string = [NSString stringWithFormat: @"%10.2f", d];
	return string;
}
The following seems to work OK to give me the thousands separators and seems to work OK for other locales (such as France where the thousand separator is a space and the decimal point is a comma).

Code:
NSString* doubleToCurrencyString(double d) {
	
	// convert the double to an NSNumber
	NSNumber *number = [NSNumber numberWithDouble: d];
	
	// create a number formatter object
	NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
	[formatter setNumberStyle: kCFNumberFormatterCurrencyStyle];
	[formatter setCurrencySymbol:  @""];
	
	// convert the number to a string
	NSString *string = [formatter stringFromNumber: number];
	
	// release just the formatter (the number will be release in the autorelease pool)
	[formatter release];
	
	// return the string
	return string;
}
kaydell.leavitt is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Double clocks, double unlock slider after ios7 upgrade MobsterMac iPhone 0 Sep 22, 2013 02:03 PM
NSString to NSSize larswik Mac Programming 1 May 28, 2013 06:54 PM
Why can't convert NSString to NSDate? mikezang iPhone/iPad Programming 4 Jan 22, 2013 06:56 PM
c++ double larger (double x, double y) rockclimber9up Mac Programming 12 Sep 2, 2012 02:17 AM
(NSString*) ?? thedon1 iPhone/iPad Programming 6 Jul 2, 2012 07:52 PM

Forum Jump

All times are GMT -5. The time now is 04:37 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC