4GetFullOf
4GetFullOf

Reputation: 1738

Error handling to check if float is .02f

Hi in my settings for an app the user must input a number that has 2 decimal digits no more or no less. So values such as 125.22, 11.09, 63.88 are acceptable values such as 1.1, 23.1823, 12, 293.0001 are not. How can I make an if statement checking if these floats values are inputted correctly?

Upvotes: 0

Views: 111

Answers (1)

SonicARG
SonicARG

Reputation: 517

Regular expressions can come handy in this situation. You can convert the number to string and match against:

[0-9]+\.[0-9][0-9]

That is:

[0-9] : Match a character, which is inside the group 0--9 -> 0123456789
+     : Between 1+ times, as much as possible
\.    : Match a dot character
[0-9] : Match a character, which is inside the group 0--9 -> 0123456789
[0-9] : Match a character, which is inside the group 0--9 -> 0123456789

Honestly, my experience in Objective-C (and Macs) is NULL, but you can have a look here on how to create and test regular expressions objects against strings.

Upvotes: 1

Related Questions