开发者

cast textfield value to double

开发者 https://www.devze.com 2023-01-16 17:01 出处:网络
i want to sum to 开发者_JAVA技巧values which i get from a textfiel. how can i cast a textfield value in double value ?

i want to sum to 开发者_JAVA技巧values which i get from a textfiel. how can i cast a textfield value in double value ?

Regards Caglar


See this question: How to do string conversions in Objective-C?

double myDouble = [myTextField.text doubleValue];


You can use doubleValue to convert a NSString (from your text field) to a double like so:

double myDouble = [myTextField.text doubleValue]; 

This isn't a cast, by the way, it's a conversion. The doubleValue method performs a conversion from a string to a double representation. Casts are something you can only perform on primitive numeric types or pointers in Objective-C.


convert text entered in textfield to integer

double mydouble=[_myTextfield.text doubleValue];

rounding to the nearest double

mydouble=(round(mydouble));

rounding to the nearest int(considering only positive values)

int myint=(int)(mydouble);

converting from double to string

myLabel.text=[NSString stringWithFormat:@"%f",mydouble];

or

NSString *mystring=[NSString stringWithFormat:@"%f",mydouble];

converting from int to string

myLabel.text=[NSString stringWithFormat:@"%d",myint];

or

NSString *mystring=[NSString stringWithFormat:@"%f",mydouble];
0

精彩评论

暂无评论...
验证码 换一张
取 消