Custom Number Formatters?

Discussion in 'Mac Programming' started by BadWolf13, Feb 22, 2010.

  1. BadWolf13 macrumors 6502

    Joined:
    Dec 17, 2009
    #1
    Is there a way to make a custom number formatter? Basically I want to display a phone number, which is essentially a 10 digit number(more if you count the extension), but I want it to display as (123)456-7890 instead of 1234567890. The only thought I had so far is to use a description method, which I'm not even sure will work with bindings.
     
  2. asciimov macrumors newbie

    Joined:
    Jan 31, 2008
    #2
    Sure thing but in what language?
     
  3. autorelease macrumors regular

    Joined:
    Oct 13, 2008
    Location:
    Achewood, CA
    #4
    I hope you're storing phone numbers as strings and not as integers!

    Hint: what would happen if I wanted to store the number (555) 555-5555? Or an international number that starts with 011?
     
  4. BadWolf13 thread starter macrumors 6502

    Joined:
    Dec 17, 2009
    #5
    My thinking was to store 5555555555 as an int, and use a custom formatter to display it as (555)555-5555, uses less memory, and that way the user can just enter 5555555555, hit enter, and have it self format. Also has the added benefit of returning an error if the user makes a mistake and types in a non-numerical character. Is there something I'm not seeing that would benefit from the methods of NSString?
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    Location:
    Sailing beyond the sunset
    #6
    Other things to think about:

    1. Will 5555555555 fit into an int? Will all phone numbers from anywhere in the world fit into an int?

    2. How much data do you have, that the difference between storing phone numbers as integers instead of strings is so significant that you have to consider it this early? (Smells of premature optimization, and at the cost of correctness).
     
  6. autorelease macrumors regular

    Joined:
    Oct 13, 2008
    Location:
    Achewood, CA
    #7
    Okay, let me be more specific. What would happen if I wanted to enter the phone number (429) 496-7296?

    If that number doesn't ring any bells to you, you've got some programming basics to acquaint yourself with.
     
  7. BadWolf13 thread starter macrumors 6502

    Joined:
    Dec 17, 2009
    #8
    Chown, you may be right. I do tend to be a miser for memory, but this does program have the potential to be used over networks. Given that 10 digits probably needs a long in, is the difference in memory significant?

    Autorelease, my thinking is that the user won't enter the (, ) or the -. That's how every program I've encountered so far works. You enter the digits, and it autoformats them for you.

    The key to good customer service is not asking the customer to do something that you yourself can do for them. I look at a user-friendly program in the same light.
     
  8. BadWolf13 thread starter macrumors 6502

    Joined:
    Dec 17, 2009
    #9
    Should I assume you're referring to the maximum value for an integer? If you are, then yes, it's been over a decade since I wrote any programs, and thus I don't know what the max values for numeric variables are anymore.
     
  9. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    Location:
    Sailing beyond the sunset
    #10
    From a user's perspective, auto-formatting a string as a phone number is no different than auto-formatting an integer.

    I also think you should look more closely at how networks actually work. The last time I had to worry about "size on the wire", I solved it by using compression (gzip), not by being concerned about the integer vs. string issue. In reality, size on the wire may not matter in a lot of cases anyway. It's only when going through a very narrow pipe (bandwidth-wise) that it matters. How narrow is that pipe, really? Are you sure size on the wire (bandwidth) is really the deciding factor, and not latency?
     
  10. BadWolf13 thread starter macrumors 6502

    Joined:
    Dec 17, 2009
    #11
    Thanks. From a programming perspective though, can I make an NSFormatter subclass like it was suggested above, and bind the textfield to a string instead of an int? I thought those formatters only worked on numbers, so please correct me if I'm wrong.

    In terms of memory, I haven't gotten far enough in the writing of this program to test it and see any lag problems, but being a user of similar programs in the past, I did experience a lot of problems. You may be right that I'm sacrificing accuracy for memory usage.
     
  11. autorelease macrumors regular

    Joined:
    Oct 13, 2008
    Location:
    Achewood, CA
    #12
    You can use NSValueTransformer and some simple string manipulation to convert a string from "1234567890" to "(123) 456-7890." Plus, NSValueTransformers work with bindings as well. It's also possible to add validation to an NSTextField, so that non-numeric characters are rejected.

    In fact, it would probably be more expensive to do the conversion from a long integer, since you'd have to convert the integer to a string and then slice it up. And that number you typed into the text field was originally a string, so you'd really be going from string -> integer -> string -> formatted string!

    In general, a series of digits that has no mathematical significance (like a phone number or zip code) should always be stored as a string. This is the way it's always been done.
     
  12. BadWolf13 thread starter macrumors 6502

    Joined:
    Dec 17, 2009
    #13
    Thank you. That helps me loads. I guess my only remaining question, in your experienced opinion, what's better, a value transformer, or a subclass of NSFormatter?
     
  13. plinden macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #14
    I would be inclined to say an NSFormatter is more appropriate. You're just presenting the phone number to the user, not transforming from one class to another. You can connect your NSTextField to an instance of the formatter in Interface Builder to handle the formatting.

    It also seems you're limiting the use of this to North American phone numbers, so you should make sure you know the format - http://en.wikipedia.org/wiki/North_American_Numbering_Plan

    For instance, the country code is always '1', the area code never starts with '1' etc.
     
  14. BadWolf13 thread starter macrumors 6502

    Joined:
    Dec 17, 2009
    #15
    Thanks Plinden. The first version will likely be limited to North American phone numbers. I may add international numbers in future versions.

    My thinking on the subject of a leading 1, I was thinking of an IF statement to remove it from the string if the user entered it, and thus display it as just (555)555-5555 instead of 1(555)555-5555, since people rarely care about the leading 1. If my understanding of NSFormatting is correct, that would go into the getObjectValue:forString:errorDescription method. Is that correct?
     

Share This Page