Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

kaydell.leavitt

macrumors regular
Original poster
Apr 19, 2010
100
0
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;
}
 

zed2

macrumors 6502a
Jul 17, 2004
606
59
Bucks
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];
 

kaydell.leavitt

macrumors regular
Original poster
Apr 19, 2010
100
0
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;
}
 

zed2

macrumors 6502a
Jul 17, 2004
606
59
Bucks
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;
}
 

kaydell.leavitt

macrumors regular
Original poster
Apr 19, 2010
100
0
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 :):):)
 

Guiyon

macrumors 6502a
Mar 19, 2008
771
4
Cambridge, 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];
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
And if you're going to use NSScanner, make sure you check scanDouble's return value and handle it if it's not successful.
 

bredell

macrumors regular
Mar 30, 2008
127
1
Uppsala, Sweden
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.
 

kaydell.leavitt

macrumors regular
Original poster
Apr 19, 2010
100
0
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

macrumors regular
Original poster
Apr 19, 2010
100
0
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 :rolleyes:
 

bredell

macrumors regular
Mar 30, 2008
127
1
Uppsala, Sweden
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.
 

kaydell.leavitt

macrumors regular
Original poster
Apr 19, 2010
100
0
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;
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.