How to use currencyStyle number as double value iphone
I have a rupees with currency symbol in a text box for ex : Rs 10,500.50 and i want to use it in a calculation as 10500*开发者_如何学JAVA5. How can i use this. when i try to convert it into double value then it returns 0.00. please suggest.
Thanks
If you've used an NSNumberFormatter to format the text string, you can use the same formatter to parse the text string back into a number.
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSNumber *rupees = [NSNUmber numberWithFloat:10500.50];
NSString *formattedRupees = [formatter stringFromNumber:rupees];
NSNumber *rupeesFromString = [formatter numberFromString:formattedRupees];
[formatter release];
If you're not using an NSNumberFormatter, you could try using an NSScanner to parse the string manually.
Careful with Float numbers!!
Using NSNumber *rupees = [NSNUmber numberWithFloat:10500.50];
may cause precision issues in the conversion. The '10500.50f' number may work, but if you try something like 25.63f, this might happen:
rupees = 25.629999
That's why using floats and doubles in currencies is not recommended.
Moving to your question:
you can do something like this:
NSString *string = @"Rs 10,500.50";
string = [string stringByReplacingOccurrencesOfString:@"," withString:@""];
string = [string stringByReplacingOccurrencesOfString:@"Rs " withString:@""];
NSArray *valueArray = [string componentsSeparatedByString:@"."];
NSInteger firstNumber = [[valueArray firstObject] intValue];
NSInteger secondNumber = [[valueArray lastObject] intValue];
And then you can use both values (firstNumber
and secondNumber
)
精彩评论