float to xx.x to UILabel.text

Discussion in 'iOS Programming' started by just66horns, Feb 5, 2010.

  1. just66horns macrumors newbie

    Joined:
    May 6, 2009
    Location:
    Denver CO
    #1
    Hi.

    I want to display in my apps view the results of a calculation, in a UILabel.

    The calculations are being handled as floats. The error I am getting is
    "Incompatible type for argument 1 of setText:"

    What am I doing wrong... I know it is related to mishandling of the variable type. How do I get the float to display in the UILabel as xx.x (one digit after the decimal?).

    Code:
    -(BOOL) textFieldShouldReturn: (UITextField *) textFeildA{
    	[textFeildA resignFirstResponder];
    	if (moveViewUp) [self scrollTheView:NO];
    	float a = [textFieldA.text floatValue];
    	float b = [textFieldB.text floatValue];
    	float fy = a / b;  
    	[self updateDisplay:fy];
    	return YES;
    }
    - (IBAction) updateDisplay: (float) e
    {
    	[labelAoverB setText: e];
    }
    
    Thanks!
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
  3. just66horns thread starter macrumors newbie

    Joined:
    May 6, 2009
    Location:
    Denver CO
    #3
    hmmm

    OK, I don't get it.
    How can I take a float, and convert it to use for the string required for the setText of a UILabel?
    Please, more help required.
     
  4. ChOas macrumors regular

    Joined:
    Nov 24, 2006
    Location:
    The Netherlands
    #4
    [NSString stringWithFormat:mad:"%.01f",lazy]
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    What, specifically, is it that you don't get?

    The very first example in the section of the document I linked you to shows you how to do just that. Please read it.

    Please see above.
     
  6. just66horns thread starter macrumors newbie

    Joined:
    May 6, 2009
    Location:
    Denver CO
    #6
    OK, I get it... Thanks

    this worked... My syntax is still poor...
    Code:
    -(BOOL) textFieldShouldReturn: (UITextField *) textFeildA{
    	[textFeildA resignFirstResponder];
    	if (moveViewUp) [self scrollTheView:NO];
    	float a = [textFieldA.text floatValue];
    	float b = [textFieldB.text floatValue];
    	float fy = a / b;  
    	[self updateDisplay:fy];
    	return YES;
    }
    - (IBAction) updateDisplay: (float) e
    {
    	[labelAoverB setText:[NSString stringWithFormat:@"%.01f",e]];
    }
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    .01? How many digits after the radix character are you trying to display with that? If you want 2, then you can just use 2, as in "%.2f".
     
  8. ChOas macrumors regular

    Joined:
    Nov 24, 2006
    Location:
    The Netherlands
    #8
    I know, but the guy wanted 1: float to xx.x to UILabel.text
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    Ah, then they probably would want to use something like @"%0.1f". (Maybe that's what you meant?)
     
  10. ChOas macrumors regular

    Joined:
    Nov 24, 2006
    Location:
    The Netherlands
    #10
    Actually I meant %.1f, somehow I was thinking of 0-leading ints.

    (probably from the xx part (good band, btw))

    Anyways, for precision 01 an 1 are the same, so no harm done.
     
  11. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #11
    True dat.
     

Share This Page