PDA

View Full Version : doubleValue on iphoneSDK?




hintzsche
Jul 28, 2009, 12:55 PM
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 ?



kainjow
Jul 28, 2009, 02:05 PM
Try: double input = [inputTextField.text doubleValue];
double factor = [factorTextField.text doubleValue];
double result = input * factor;
outputTextField.text = [NSString stringWithFormat:@"%f", result];

hintzsche
Jul 28, 2009, 02:57 PM
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 ?

moral-hazard
Jul 28, 2009, 08:55 PM
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 ?

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.

hintzsche
Jul 29, 2009, 08:49 AM
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 ?

dejo
Jul 29, 2009, 10:09 AM
According to the Class Reference for NSString, doubleValue:
This method uses formatting information stored in the non-localized value; use an NSScanner object for localized scanning of numeric values from a string.

hintzsche
Jul 29, 2009, 10:39 AM
how do i do this ? isnt this uiscanner on iphone ?

dejo
Jul 29, 2009, 10:50 AM
how do i do this ? isnt this uiscanner on iphone ?
NSScanner (http://developer.apple.com/iPhone/library/documentation/Cocoa/Reference/Foundation/Classes/NSScanner_Class/Reference/Reference.html)

No, it's still NSScanner, as it's part of the Foundation Framework.

hintzsche
Jul 29, 2009, 11:25 AM
NSScanner (http://developer.apple.com/iPhone/library/documentation/Cocoa/Reference/Foundation/Classes/NSScanner_Class/Reference/Reference.html)

No, it's still NSScanner, as it's part of the Foundation Framework.

- (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 ?

dejo
Jul 29, 2009, 11:27 AM
where do i have to place the NSScanner ?
You need to use it in place of the doubleValue calls.

hintzsche
Jul 29, 2009, 12:14 PM
do you mean NSScanner input = [inputTextField.text doubleValue];

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

???

do i have to do something more ?

dejo
Jul 29, 2009, 12:19 PM
do i have to do something more ?
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.)

PhoneyDeveloper
Jul 29, 2009, 02:20 PM
I believe that you can use NSNumberFormatter numberFromString to parse float values in a locale-dependent manner.