Does anyone have a more elegant way of checking that a number has no more than two places after the decimal point than this example:-
if STR$(tempEval * 100) <> STR$(tempEval * 100) then print #Foz.info, "Too many decimal places for empty value!": valerr = 1: return
It just seems long-winded....
Checking a numerical value
Re: Checking a numerical value
So you want to check that there are no more than two (non-zero) digits after the decimal point? If you can guarantee that there is a decimal point (i.e. the number isn't an integer), or that if it is an integer it has fewer than three digits, one way would be:Paul Gorton wrote: ↑Sun Sep 28, 2025 4:32 pm Does anyone have a more elegant way of checking that a number has no more than two places after the decimal point than this example:-
Code: Select all
n$ = STR$(tempEval)
IF LEN(n$) > INSTR(n$, ".") + 2 THEN PRINT "Too many decimal places"
If the number might be an integer with more than 2 digits (or a negative integer with more than one digit) this would have to be modified.
-
- Posts: 4
- Joined: Sun Sep 28, 2025 11:24 am
Re: Checking a numerical value
Unfortunately, there is no control, only instructions, regarding what the user should enter. There are, however, other checks that come before this to eliminate text, numbers that are too great, small or negative but nothing to guarantee a decimal point,
Re: Checking a numerical value
You might just chop off the excess. Explore using() and see if it helps.
n=12345
print using("#####.##",n)
n=12345.666
print using("#####.##",n)
n=-.666
print using("#####.##",n)
n=12345.6
print using("#####.##",n)
n=12345
print using("#####.##",n)
n=12345.666
print using("#####.##",n)
n=-.666
print using("#####.##",n)
n=12345.6
print using("#####.##",n)
-
- Posts: 4
- Joined: Sun Sep 28, 2025 11:24 am
Re: Checking a numerical value
Thanks for that. I'll give it a whirl.
Re: Checking a numerical value
I said it needs to have a decimal point or to be a positive integer of no more than two digits. So if there are already checks that the number is positive and not too large, my method should work. I'm not clear why you seem to have dismissed it as unsuitable.Paul Gorton wrote: ↑Sun Sep 28, 2025 9:12 pm There are, however, other checks that come before this to eliminate text, numbers that are too great, small or negative but nothing to guarantee a decimal point,
-
- Posts: 4
- Joined: Sun Sep 28, 2025 11:24 am
Re: Checking a numerical value
You are absolutely correct, my error, oops!