0
Use of isdigit
How to allow decimal point only for 1~2 decimal places when using isdigit? My current code is as below: for (i = 0; i < length; i++) { //validations allowing last character is '\n' or '\0' if (!isdigit(a)) { //Display invalid } }
3 Antworten
+ 2
It's _easier_ to let the user enter whatever they want, then clip it:
sprintf(buf, "%.2f", user_input);
You can also just copy user input byte by byte from one buffer to the other, omitting non-digits and post .xx if you want. If you need it accessed as a float, just cast it so.
Do you need an example code?
0
What're you asking??
Do you want to limit a number to 2 decimal points? What's \n and 0 got to do with it? And "isdigit(a)"? Please show me what your end goal is.
0
I want to limit a number to 1 or 2 decimal places. E.g. 2.8 or 2.85
Because isdigit rejects \n and \0 so I make it if the last character is \n or \0, then allow. Before that I already reject the condition when the first character is \n or \0 so that when user only enter without key in any numbers or characters, it will show invalid
isdigit(a), the 'a' inside is a variable name
My end goal is to allow decimal point, but reject it if it is first character or last character or exceed 2 decimal places