Nari
Nari

Reputation: 213

How to format a currency string to float value?

Iam getting the string like this $2,000.00. Now i want to change this string to float value. How to convert this string as float.Actually this string($2,000.00)is coming from formatting number with currency formatter. But now i want to change this string to float value..

Please guide anyone for this.

Thanking in advance.

Upvotes: 3

Views: 2092

Answers (3)

EmptyStack
EmptyStack

Reputation: 51374

Consider the string.

    NSString *str = @"$2,000.00";

Create a number formatter.

    NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];

The "important thing".

    [nf setNumberStyle:NSNumberFormatterCurrencyStyle];

"Here is what you want".

    NSNumber *number = [nf numberFromString:str];
    float f = [number floatValue];

And of course, release the number formatter.

    [nf release];

Upvotes: 10

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

You could use the same formatter to get an NSNumber object representing the float value using numberFromString:. The call floatValue on the NSNumber object.

Upvotes: 0

Stark
Stark

Reputation: 441

NSString objects are the ones that have methods like floatValue. But after removing the $ sign from the string.

Upvotes: 0

Related Questions