Handling comma in dollar amounts?

Discussion in 'iOS Programming' started by nottooshabby, Aug 7, 2008.

  1. nottooshabby macrumors 6502

    Joined:
    Jul 12, 2008
    #1
    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?
     
  2. detz macrumors 65816

    Joined:
    Jun 29, 2007
  3. TEG macrumors 604

    TEG

    Joined:
    Jan 21, 2002
    Location:
    Langley, Washington
    #3
    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
     
  4. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #4
    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.
     
  5. nottooshabby thread starter macrumors 6502

    Joined:
    Jul 12, 2008
    #5
    cool, I'll give it a try...
     

Share This Page