Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nottooshabby

macrumors 6502
Original poster
Jul 12, 2008
416
90
So I am accepting input from the user for a program I am working on. I would like to make it robust and be able to handle the case where somebody puts a comma in the dollar amount, for example $40,000. My program can handle input with either $40000 or 40000 but the comma makes it barf. Is there any easy way to parse this out so that I get a float value of 40000 from $40,000?
 

TEG

macrumors 604
Jan 21, 2002
6,625
173
Langley, Washington
I can't give you specific answers, but I can give you some pointers.

In the API there should be a way to find out the money symbol, decimal symbol, and seperator (and likely how many digits between the seperators). As most of the world does it differently to how the English speaking world does numbers. So some will use "." to seperate thousands and "," to indicate a decimal. And other language use a different number of places between seperators.

I would search on the Objective C API or maybe SDK specific calls for decimal or seperator to find the answer. Then parse the number and remove the char.

Or you could just parse the number and remove any non-int char, comparing to the number's ASCII or Unicode value.

TEG
 

admanimal

macrumors 68040
Apr 22, 2005
3,531
2
All you need to do is use an NSNumberFormatter. It will handle all of the parsing/printing for you, and as long as you use it properly it will automatically configure itself to work with whatever the local currency is.
 

nottooshabby

macrumors 6502
Original poster
Jul 12, 2008
416
90
All you need to do is use an NSNumberFormatter. It will handle all of the parsing/printing for you, and as long as you use it properly it will automatically configure itself to work with whatever the local currency is.

cool, I'll give it a try...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.