5

My goal is simple, I have a textfield storing a localized currency value ($1,000.00 for instance) and I need to get that value back from the textfield as a float to perform some test on the textfield.

The code is really simple, first I format the NSNumber to a localizedString as so :

NSNumberFormatter* formatter = [[NSNumberFormatter alloc] init];
formatter.locale = [NSLocale currentLocale];
formatter.numberStyle = NSNumberFormatterCurrencyStyle;
textfield.text = [formatter stringFromNumber:number];

Everything works fine, but then I do the opposite to get the doubleValue from this localized string :

NSNumberFormatter* formatter = [[NSNumberFormatter alloc] init];
formatter.locale = [NSLocale currentLocale];
formatter.numberStyle = NSNumberFormatterCurrencyStyle; //also tested with NSNumberFormatterDecimalStyle

double d = [formatter numberFromString:textfield.text].doubleValue;

The problem is that the formatter only returns the integer value of my string and not the decimals, worse, if it is a canadian localizedstring (20 000,00 $) it only returns 20.

I've been searching for some time now and can't find anything about this behavior. The workaround is of course storing the NSNumber right before formating it to the textfield and then testing this nsnumber, but I really don't find it pretty.

brandonscript
  • 68,675
  • 32
  • 163
  • 220
nebuto
  • 994
  • 2
  • 9
  • 16
  • 1
    You can see this [Obtaining an NSDecimalNumber from a locale specific string][1] about [1]: http://stackoverflow.com/questions/317311/obtaining-an-nsdecimalnumber-from-a-locale-specific-string – Giuseppe Mosca Nov 14 '12 at 13:01

3 Answers3

3

In Swift, you can do this by similarly setting the locale to the format of the currency, and then getting the double:

    var locale = NSLocale.currentLocale()
    var formatter = NSNumberFormatter()
    formatter.locale = NSLocale(localeIdentifier: "en_US")
    formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle

    var moneyDouble = formatter.numberFromString(textField.text)?.doubleValue
Gabriel Garrett
  • 2,087
  • 6
  • 27
  • 45
1

The answer, as pointed by Giuseppe, is really simple and I'm bashing my head against the wall for not thinking about it.

double d = [NSDecimalNumber decimalNumberWithString:textfield.text locale:[NSLocale currentLocale]].doubleValue;
nebuto
  • 994
  • 2
  • 9
  • 16
  • If your original price string includes a currency sign (e.g: @"$9.99"), then zvtra's answer is the correct (Using the NSNumberFormatter). – dornad Jan 30 '15 at 20:52
0

A slight enhancement to zavtra's answer that returns a float and doesn't force the locale identifier to be en_US (Swift 2):

func floatFromTextFieldText() -> Float {
    let formatter = NSNumberFormatter()
    formatter.locale = NSLocale.currentLocale()
    formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
    guard let amount = self.text else { return 0.0 }
    return formatter.numberFromString(amount)!.floatValue
}
brandonscript
  • 68,675
  • 32
  • 163
  • 220