Checking a numerical value

You can talk about anything related to LB Booster here, not covered in another category
Paul Gorton
Posts: 4
Joined: Sun Sep 28, 2025 11:24 am

Checking a numerical value

Post by Paul Gorton »

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....
guest
Site Admin
Posts: 227
Joined: Tue Apr 03, 2018 1:34 pm

Re: Checking a numerical value

Post by guest »

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:-
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:

Code: Select all

n$ = STR$(tempEval)
IF LEN(n$) > INSTR(n$, ".") + 2 THEN PRINT "Too many decimal places"
This assumes that the rounding applied by STR$ will take care of small errors in the decimal > binary > decimal conversion, which it probably will but it should be used with care.

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.
Paul Gorton
Posts: 4
Joined: Sun Sep 28, 2025 11:24 am

Re: Checking a numerical value

Post by Paul Gorton »

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,
Rod
Posts: 17
Joined: Fri Apr 06, 2018 7:00 am

Re: Checking a numerical value

Post by Rod »

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)
Paul Gorton
Posts: 4
Joined: Sun Sep 28, 2025 11:24 am

Re: Checking a numerical value

Post by Paul Gorton »

Thanks for that. I'll give it a whirl.
guest
Site Admin
Posts: 227
Joined: Tue Apr 03, 2018 1:34 pm

Re: Checking a numerical value

Post by guest »

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,
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
Posts: 4
Joined: Sun Sep 28, 2025 11:24 am

Re: Checking a numerical value

Post by Paul Gorton »

You are absolutely correct, my error, oops!