Removing zeros from float in a textfield

Discussion in 'Mac Programming' started by JGoose, Feb 13, 2010.

  1. JGoose macrumors member

    Joined:
    Feb 12, 2010
    #1
    How do I remove the unnecessary zeros after the decimal point when a float is displayed in a textfield?
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
  3. JGoose thread starter macrumors member

    Joined:
    Feb 12, 2010
    #3
    okay, so I have that working now, but how do I get it to display a zero (after the decimal) inputted by the user.

    example: user inputs "1.06"

    The "0" only shows after the user inputs the "6"
    I want it to display the "0" whether or not the user inputs another digit.
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    Read the options for the class robbieduncan linked to. Specifically as related to fraction digits. Specifically something that might "set" the "minimum fraction digits".

    -Lee
     
  5. JGoose thread starter macrumors member

    Joined:
    Feb 12, 2010
    #5
    the formatter's "minimum fraction digit" is already set to zero in IB if that's what your talking about.
     
  6. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    So if you always want at least 1, is 0 the right setting?

    -Lee
     
  7. JGoose thread starter macrumors member

    Joined:
    Feb 12, 2010
    #7
    Well, that's not what I want though. I want zeros to appear only if the user inputs them. Simply so that they know that they did and that the computer received their input. Basically what the formatter is doing right now is not displaying a zero until it knows that it is a significant digit.
     
  8. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    Then write your own custom formatter that does what you want...
     
  9. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #9
    Umm, why not just display the string the user inputs then, and handle it like a double internally?
     
  10. mdeh macrumors 6502

    Joined:
    Jan 3, 2009
    #10
    ....or....look at Chapter 26 of Hillegass...that should get you started. ( You seem to be making your way through Hillegass).
     
  11. JGoose thread starter macrumors member

    Joined:
    Feb 12, 2010
    #11
    Yes, that's what I am doing. first, I read Kochan's "Programming In Objective-C 2.0" and now I'm reading Hillegass. And in the mean time I figured I'd try to make a calculator from scratch on my own as I learned how to do each thing necessary in a calculator. It has actually really helped get a good grasp of the concepts.

    I have been able to finish it but there is still this one thing bothering me. I'm still in chapter 17, but I'll take a look at 26. Thanks, mdeh.

    That is what I was doing, but since it is a calculator, the program displays the result in double format with all the extra zeros, which I also want to avoid.
     
  12. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #12
    Ok, in that case, why not just leave the string the user inputs UNTIL they actually calculate something, then replace the string with NSNumberFormatter stringFromNumber: version.
     
  13. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #13
    Which means you will have to do your own filtering on input so that you only get numerics and action keys.
     
  14. mdeh macrumors 6502

    Joined:
    Jan 3, 2009
    #14
    Agreed, 100%. It seems pretty pointless rushing through Hillegass and not really achieving anything other than having rushed through Hillegass at the end of the book. This forum is a wonderful setting to get answers...but I must stress ...**not homework** answered. You are expected to at least show that you are trying to understand.
    BTW...I went back and redid the Chap 14 challenge and answered your questions...not sure if you saw that.
     
  15. JGoose thread starter macrumors member

    Joined:
    Feb 12, 2010
    #15
    Yes, I did, thanks.

    Sounds like a good idea, I'll try that.
     
  16. JGoose thread starter macrumors member

    Joined:
    Feb 12, 2010

Share This Page