Float to 2 decimal places and firstResponder of a text field

Discussion in 'iOS Development' started by Chicken, Feb 28, 2009.

  1. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    I just want to know how I can make a float value be rounded to 2 decimal places. At the moment I have
    Code:
    number = [NSString stringWithFormat:@"%f", myAmount
    but it has 6 zeros after the decimal place I want this to be only 2.

    My second question is as you know when you touch a UITextField it becomes the first responder. Well my keyboard partially covers the UITextField I want it to scroll up automatically, as seen in many App Store applications. I take it that this would be in the firstResponder function. Do you know how ?

    Thanks for your help!
    ------------------double post merged------------------
    Ok I fixed the first problem by using

    Code:
    number = [NSString stringWithFormat:@"%.2f", myamount];
    I still don't have an answer for the second question though.
  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    1. You're not setting the precision with that; you're only changing the display. This is a huge difference when you go to actually do math with this number. Look into math.h to properly truncate your number.
    2. Make sure you're setting something as a UITextFieldDelegate. Then you can just override the didBecomeFirstResponder (making sure to also call [super didBecomeFirstResponder]).
  3. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    Ok Skylar I only really need to change the display of it. So that is fine. I know how to override the didBecomeFirstResponder. I just want to know how I can make it scroll up so you can view the full text field.
  4. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    You must scroll the UIView up, or the TextField(s), however you choose. I used an animation to do this, it worked quite well.
  5. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    Hey stephen.4 I know I should find a way to do it myself. But could you please send me the code?
  6. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
  7. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    Thank You!

Share This Page