Reputation: 83
From my textfield
I get a string
like 25,50
.
I want to convert this string
into a float
. But if I use myFloat = [mytextfiel.text floatValue];
I just get 25.00
.
Is there a method to get a float like 25.50
?
Upvotes: 0
Views: 1146
Reputation: 1189
Alternatively, you can use NSLocale
to determine what to do and accomplish it with NSScanner in case you have no access to NSNumberFormatter
:
1.000.000,00
-> 1000000,00@"%@.%@"
.This will parse out virtually any case of use, i.e. 1.500,50€ in German, 1,500€50 in French, $1500.50 in English will all result in number 1500.5
Upvotes: 1
Reputation: 13864
Replacing the commna with a period works, but you can also use an NSNumberFormatter
and set your locale appropriately (note: untested code):
NSNumberFormatter * formatter = [[NSNumberFormatter alloc] init];
formatter.locale = [NSLocale currentLocale];
formatter.localizesFormat = YES;
float myFloat = [[formatter numberFromString:string] floatValue];
Upvotes: 5
Reputation: 1730
try this:
float floatNo = [[@"25.50" stringByReplacingOccurrencesOfString:@"," withString:@"."] floatValue];
NSLog(@"%f",floatNo);
O/P
25.500000
Upvotes: 0
Reputation: 949
just replace the comma with a dot when you extract the float
myFloat = [[mytextfiel.text stringByReplacingOccurrencesOfString:@"," withString:@"."] floatValue];
Upvotes: 2
Reputation: 123
Floats must follow the decimal point format (no matter which region of the world), it means that the final product of your string must have a . instead of the comma. I suggest you replace it before storing in a database or before processing the value.
Upvotes: 0