Will an iOS app calculate numbers when a comma is used instead of a decimal point?

New to iOS, and I'm just making a small calculator for the starter. I am studying various features, including an application available in France and other non-English countries.

However, since my application is very dependent on calculating the numbers that have a decimal point in them, I want to know if iOS will calculate the number if the comma is used as a decimal point, since countries like France use this notation:

Australia / United States / Other countries:

12.5 * 15.8 = 197.5 

France / other countries:

 12,5 * 15,8 = 197,5 

Basically I ask: a) if a comma is inserted instead of a decimal point, it will still be calculated on local devices, and b) if not, what code should be included in my application so that it calculates this notation? (Note that I am not asking for a comma as a separator, but what acts as the equivalent of a decimal point.)

Thank you in advance!

+4
source share
3 answers

In fact, iOS has a handy class built in to handle the localization of numeric values. It was called NSNumberFormatter , and it specifically has - setDecimalSeparator:.

+2
source

Before using a shape, you must "normalize" it by translating it into NSNumber. Here's how:

 NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; [formatter setNumberStyle:NSNumberFormatterDecimalStyle]; NSNumber *myNumber = [formatter numberFromString:@"197.5"]; 

This is the right way to do this, and with this method regional preferences, comma delimiters, etc. will be taken into account.

At this point, myNumber can be used as decimal, float, integer, etc.

 float myFloat = [myNumber floatValue]; 
+1
source

Will my iOS application calculate numbers if a comma is used instead of a decimal point?

Yes. It will do this transparently enough while you are using NSNumberFormatter .

I want to know if iOS will calculate the figure if the comma is used as a decimal point, as countries like France use this notation ...

Information on how to format data such as numbers and dates is controlled by the NSLocale class. NSNumberFormatter uses the current locale to figure out how to interpret and display numbers, so when you use the -numberFromString: method, the formatter will use the appropriate decimal separator. Same thing when you create a string representation of a number with -stringFromNumber:

For your purposes, you probably don't need to worry about locales. Just use the number formulator to interpret the input and create the output, and the formatter will use the locale that is appropriate for the user. However, if you want your application to provide information localized in a language or region other than the user, you can use the -setLocale: method for installation to use a specific locale.

+1
source

Source: https://habr.com/ru/post/1414822/


All Articles