How Do I Convert from an NSString to a double?

Discussion in 'iOS Programming' started by kaydell.leavitt, Apr 20, 2010.

  1. macrumors regular

    Joined:
    Apr 19, 2010
    #1
    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;
    }
     
  2. macrumors 6502a

    Joined:
    Jul 17, 2004
    Location:
    Bucks
    #2


    double *cost = [string doubleValue];
     
  3. thread starter macrumors regular

    Joined:
    Apr 19, 2010
    #3
    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;
    }
     
  4. macrumors 6502a

    Joined:
    Jul 17, 2004
    Location:
    Bucks
    #4
    Code:
    - (double) getCost {
    NSString *string = @"2.0";
    double *cost = 0.0;
    if ([string scanDouble]) {
       cost = [string doubleValue];
    }
    return *cost;
    }
     
  5. thread starter macrumors regular

    Joined:
    Apr 19, 2010
    #5
    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 :):):)
     
  6. macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #6
    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];
     
  7. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #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.
     
  8. macrumors regular

    Joined:
    Mar 30, 2008
    Location:
    Uppsala, Sweden
    #8
    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.
     
  9. thread starter macrumors regular

    Joined:
    Apr 19, 2010
    #9
    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;
    }
    
     
  10. thread starter macrumors regular

    Joined:
    Apr 19, 2010
    #10
    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:
     
  11. macrumors regular

    Joined:
    Mar 30, 2008
    Location:
    Uppsala, Sweden
    #11
    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.
     
  12. thread starter macrumors regular

    Joined:
    Apr 19, 2010
    #12
    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;
    }
    
     

Share This Page