View Full Version : Handling comma in dollar amounts?
nottooshabby
Aug 7, 2008, 03:34 PM
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?
detz
Aug 7, 2008, 03:41 PM
Search for ',' and replace it with '' :D
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
Aug 7, 2008, 09:25 PM
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
Aug 8, 2008, 08:17 AM
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...
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.