doubleValue on iphoneSDK?

Discussion in 'iOS Programming' started by hintzsche, Jul 28, 2009.

  1. hintzsche macrumors newbie

    Joined:
    Jul 28, 2009
    #1
    hi i tried to programming a app on iphone, but i got the following errors

    double input = [inputTextField doubleValue]; !incompatible types in initialization
    double factor = [factorTextField doubleValue]; !incompatible types in initialization
    double result = input * factor;
    [outputTextField setdoubleValue:result]; !UITextField may not respaont on -setdoubleValue

    can someone help me ?
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Try:
    Code:
    double input = [inputTextField.text doubleValue];
    double factor = [factorTextField.text doubleValue];
    double result = input * factor;
    outputTextField.text = [NSString stringWithFormat:@"%f", result];
     
  3. hintzsche thread starter macrumors newbie

    Joined:
    Jul 28, 2009
    #3
    Thanks but now i got the problem that the user cant type a ,

    because the programm dont count with , now
    only with .

    what can i do to make a . from a ,

    and i got more then 2 nubers after the . but i only need 2 nubers after the ,

    can someone help me ?
     
  4. moral-hazard macrumors regular

    Joined:
    Jul 27, 2009
    Location:
    Palo Alto, CA
    #4
    Are you referring to a numerical value separated by commas?
    As in "500,000" or something similar?

    Try grabbing the text as a string and removing the commas...alternatively im sure there is an apple function *somewhere* to convert a number into a string including commas and the like. Try looking at the NSString class reference.
     
  5. hintzsche thread starter macrumors newbie

    Joined:
    Jul 28, 2009
    #5
    yes i try to create a money convertion programm. and the user will type 1,35$
    are 1,00 € but when he do this the programm dont recognise the numbers after the ,

    and if the user use a . the result will be 1.3500000

    that will confuse the user :)

    so what is the solution of this problem ?
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    According to the Class Reference for NSString, doubleValue:
     
  7. hintzsche thread starter macrumors newbie

    Joined:
    Jul 28, 2009
    #7
    how do i do this ? isnt this uiscanner on iphone ?
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    NSScanner

    No, it's still NSScanner, as it's part of the Foundation Framework.
     
  9. hintzsche thread starter macrumors newbie

    Joined:
    Jul 28, 2009
    #9
    Code:
    - (IBAction)calculate:(id)sender
    {
    	double input = [inputTextField.text doubleValue];
    	double factor = [factorTextField.text doubleValue];
    	
    	double result = input * factor;
    	[outputTextField setText:[NSString stringWithFormat:@"%f",result]];
    }
    This is the code i wrote where i cant use a , and where the result was 1.3400000


    where do i have to place the NSScanner ?
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    You need to use it in place of the doubleValue calls.
     
  11. hintzsche thread starter macrumors newbie

    Joined:
    Jul 28, 2009
    #11
    do you mean NSScanner input = [inputTextField.text doubleValue];

    or do you mean double input = [inputTextField.text NSScanner];

    ???

    do i have to do something more ?
     
  12. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #12
    Yeah, you do. You'll want to read the NSScanner doc and learn how to use it. (I.E. I'm not just going to give you the solution. Sorry.)
     
  13. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #13
    I believe that you can use NSNumberFormatter numberFromString to parse float values in a locale-dependent manner.
     

Share This Page