daved
New Member
Posts: 24
|
Post by daved on Jan 15, 2023 10:00:09 GMT -5
x = 50/12
print x 'result is 4.16666667
x$ = str$(x)
print using("##.##",x$) 'result is 4.17
I need x to = 4.17 for a math calculation, but all I do results in 4.16666667 The print using("##.##",x$) result is 4.17 I've tried converting x to a string, then converting x to 4.16 (2 characters after the decimal), and adding .01 if the last character is > 5. But when I convert back to an integer, the result is 4.17666667, which is too large for an accurate calculation. How can I get 4.17?
|
|
|
Post by Walt Decker on Jan 15, 2023 10:49:32 GMT -5
Try this: X = 50 / 12 Res$ = STR$(VAL(USING("##.##", X))) PRINT Res$ X = VAL(Res$)
|
|
daved
New Member
Posts: 24
|
Post by daved on Jan 15, 2023 11:03:02 GMT -5
Perfect - That works!!
Thanks!!!
|
|
|
Post by tsh73 on Jan 15, 2023 11:21:07 GMT -5
a function that rounds to given number of decimals
for n = -5 to 5 print "Rounds to ";(n);" decimal places" for i = 1 to 4 rand=rnd(0)*1e6 x = round(rand, n) print rand, x, using ( "#########.######", x) 'without USING you'll have only 7 digits printed next next
function round(x, n) 'rounds x to n-th digith (involves 1/10^n, obviously round=int(x*10^n+.5)/10^n '+0.5 for rounding to nearest end function
|
|
honky
Junior Member
Posts: 63
|
Post by honky on Jan 15, 2023 11:36:12 GMT -5
If the third decimal is greater than 5, it is normal to round the second to the next higher unit.
|
|
|
Post by dan1101 on Jan 15, 2023 11:43:17 GMT -5
If you are just trying to kill the repeating decimal, you could cut it off at three decimal places like this:
x = 50/12
x = (INT(x*1000))/1000
print x print (USING("##.###",x)) print (USING("##.##",x))
Note that this DOES NOT ROUND the value in x, it merely concatenates after the thousandths place to kill the repeating decimal. Keep in mind your new value will be accurate to only TWO decimal places, not three, because the thousandths place was not rounded. The result is 4.166, which eliminates the repeating decimal, and remains accurate to the hundredths (but not the thousandths) place. When you print it with USING, it will round correctly to two places and will display 4.17. If you need greater precision, you can simply concatenate further to the right of the decimal by multiplying and dividing by 10000 or more.
|
|
daved
New Member
Posts: 24
|
Post by daved on Jan 15, 2023 12:33:45 GMT -5
Thanks tsh73, I'll keep this code in my templates folder for future use. Also thanks dan1101. The code from Walt Decker was exactly what I needed and I used that in my program, which now gives the results I expected.
|
|
|
Post by Walt Decker on Jan 15, 2023 13:51:42 GMT -5
Although I do not use LB for rounding decimal numbers, here are some functions that can be used: ' DecNum = 500.765 / 13.176 DecNum = -1 * DecNum PRINT DecNum RoundVal = FN.RoundDec(DecNum, 2) PRINT RoundVal end
'----------------------------------- '-----------------------------------
FUNCTION FN.RoundDec(ValIn, NumDecimals)
DecPos = 0 ValOut = 0
ValStr$ = "" Left$ = "" LftTpl$ = "" RgtTpl$ = "" CmbTpl$ = ""
ValStr$ = STR$(ValIn) DecPos = INSTR(ValStr$, ".") IF DecPos = 0 THEN FN.RoundDec = ValIn EXIT FUNCTION END IF
Left$ = LEFT$(ValStr$, DecPos - 1) DecPos = LEN(Left$) LftTpl$ = FN.RepeatStr$(DecPos, "#") RgtTpl$ = FN.RepeatStr$(NumDecimals, "#")
CmbTpl$ = LftTpl$ + "." + RgtTpl$ ValStr$ = STR$(VAL(USING(CmbTpl$, ValIn))) ValOut = VAL(ValStr$)
FN.RoundDec = ValOut END FUNCTION
'----------------------------------- '-----------------------------------
'----------------------------------- '-----------------------------------
FUNCTION FN.RepeatStr$(RepNum, RepStr$)
StrOut$ = "" FOR I = 1 TO RepNum StrOut$ = StrOut$ + RepStr$ NEXT I
FN.RepeatStr$ = StrOut$ END FUNCTION '
|
|