I have my database in Sqlite and the开发者_Go百科 field name is Amount with float datatype.now the value of amount is entered in textfiled.so my question is how to get decimal value from textbox?
try this :
float amount = [yourTextField.text floatValue];
EDITED
yes as taskinoor mentioned you should first check weather your text field contains any alphabets or not.
to check, try this
NSString *aString = @"22.60Hello";
NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLKMNOPQRSTUVWXYZ"] invertedSet];
if ([aString rangeOfCharacterFromSet:set].location != NSNotFound)
{
NSLog(@"This string contains illegal characters");
}
Hope this helps you.
float f = [myTextField.text floatValue];
Note that you should check yourself that the text field contains a valid number. From NSString manual,
The floating-point value of the receiver’s text as a float, skipping whitespace at the beginning of the string. Returns HUGE_VAL or –HUGE_VAL on overflow, 0.0 on underflow. Also returns 0.0 if the receiver doesn’t begin with a valid text representation of a floating-point number.
This method won't tell you that the input abc
is not a valid number.
NSString *decimalvalue=[NSString stringWithString:[txtfield.text componentsSeparatedByString:@"."] objectAtIndex:1]];
Get the value from textbox and store in string. And you need to just convert this string to float value and insert in database.
this might work :
NSString *myString = (string initialization here);
float stringFloat = [myString floatValue];
float decimalNum = [textfield.text floatvalue]
精彩评论