Decimal point in calculations as . or ,

前端 未结 4 923
余生分开走
余生分开走 2021-01-23 01:48

If I use decimal pad for input of numbers the decimal changes depending of country and region format.
May be as a point \".\" or as a comma \",\"
And I do not have contr

4条回答
  •  生来不讨喜
    2021-01-23 02:42

    You shoudld use a NSNumberFormatter for this, as this can be set to handle different locales.

    Create a formatter:

    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setLocale:[NSLocale currentLocale]];
    

    Use it:

    NSNumber *number = [numberFormatter numberFromString: string]; //string is the textfield.text
    

    if the device's locale is set to a locale, where the decimal separator in a ,, the Number Keypad will use is and the formatter as-well. On those the grouping separator will be .

    For the other locales it will be vice-versa.

    NSNumberFormatter is very sophisticated, you should read its section in Data Formatter Guide, too. It also knows a lot of currency handling (displaying, not conversion), if your app does handle such.

提交回复
热议问题