-2

I want to check that input string is number, is there any way? Basically strings are recieved from text field so need to check that whether the string in number or not.

Jan49
  • 59
  • 1
  • 2
  • 8

2 Answers2

0

There are many solutions discussed in StackOverflow, take a look at:

Finding out whether a string is numeric or not

Community
  • 1
  • 1
Duyen-Hoa
  • 15,384
  • 5
  • 35
  • 44
  • 1
    Cite from: http://stackoverflow.com/questions/12238221/checking-if-nsstring-is-integer "does not check if the string contains only a valid number. It succeeds also for "123abc". " – Martin R Sep 2 '12 at 18:11 – Szu May 30 '14 at 09:41
  • Hermang: the result is an NSDecimal. The best solution is the one of John Calsbeek in posted link. – Duyen-Hoa May 30 '14 at 09:47
0
 NSString *yourString = @"...";
 [yourString doubleValue] / [yourString floatValue] - this function will return the following:

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.

you can change the keyboard input type of the text field and then you make sure that the input is valid: yourTextField.keyboardType = UIKeyboardTypeDecimalPad;

Gal Marom
  • 8,499
  • 1
  • 17
  • 19