Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Convert Currency Formatted Text to Double Value or NSNumber Value?

I want to convert currency to double value

In My app I have a textField which displays Currency Like

$1,234,567.00

I can Easily remove the $ sign by substrings

But Is there any Formatter style that can make it to the Double? Actually when I am doing

vardouble=[txtfield.text doublevalue];

because of comma it shows 1.00(above value).

like image 483
Arpit B Parekh Avatar asked Sep 08 '11 06:09

Arpit B Parekh


2 Answers

I would suggest you to use NSNumberFormatter.

NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
[nf setNumberStyle:NSNumberFormatterCurrencyStyle];
double d = [[nf numberFromString:@"$1,234,567.00"] doubleValue];
like image 101
EmptyStack Avatar answered Nov 02 '22 00:11

EmptyStack


1) First remove commas using - (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement;

2) then Call [yourString doubleValue]

like image 3
iOSPawan Avatar answered Nov 01 '22 23:11

iOSPawan