Double value validation in objective c [duplicate]
Possible Duplicates:
How to check if NSString is numeric or not iphone how to check that a string is numeric only
I would like to validate a UITextField
input.
I want that the input must be a double
, and I want to detect if there are any strange characters as "()?
etc.
Is there an easy way to detect that the input is a correct double
better than开发者_JS百科 the following?
if ([amount doubleValue] <= 0.0f)
I think you must validate "amount" string value before casting it to double, for example you can check if it contains "." or something else ... Or you do conversation in try catch and if your string will contain "(" or "()" or ... it will catch exception.
I think, the approach, which you used is very quick and simple, would suggest you to go with this only
- (double)doubleValue
Return Value description from apple doc.
The floating-point value of the receiver’s text as a double. Returns HUGE_VAL or –HUGE_VAL on overflow, 0.0 on underflow. Returns 0.0 if the receiver doesn’t begin with a valid text representation of a floating-point number.
精彩评论