Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace comma with dot when getting floatvalue of NSString

I want the users to be able to input values above 0 for a money amount in my application. So 0,0 or any representation of zero will be unacceptable but 0,1 will be accepted, for example.

In order to do this, I was planning to get the float value of NSString and compare it to 0.0 but this does not work since our decimal numbers need to be seperated with comma, always (due to a business requirement). If the commas were dots, then the comparison does the job.

What's the most decent way to replace commas with dots in my amount texts?

PS: The user is limited to enter only numbers and just one comma, for the decimal part. And actually I was wondering if this is sthg that could be done with number formatters or so...

Thx in advance

like image 745
aslisabanci Avatar asked Jul 11 '11 11:07

aslisabanci


2 Answers

Here's a neat way to use NSScanner:

    NSScanner *scanner = [NSScanner localizedScannerWithString:theInputString];
    float result;
    [scanner scanFloat:&result];
like image 159
Nestor Avatar answered Oct 27 '22 00:10

Nestor


You could just use stringByReplacingOccurrencesOfString

NSString *newString = [ammount stringByReplacingOccurrencesOfString:@"," withString:@"."];
like image 44
Tom Jefferys Avatar answered Oct 27 '22 01:10

Tom Jefferys