|
Post by honky on Apr 10, 2021 13:08:20 GMT
Good morning all,
var1=500: var2=210 rep=var1/var2 print rep '2.38095238 'rep=print using("####.##", rep) '-->syntax error '--------- rep$=str$(rep): rep$=using("####.##", rep$): rep=val(rep$) print rep '-->2.38 ok
There is no way to avoid passing by "$"? Thank for.
|
|
|
Post by Brandon Parker on Apr 10, 2021 14:04:39 GMT
The Using() function takes a numerical value as the second argument and returns a string representation of that number matching the first argument's format.
var1 = 500: var2 = 210 rep = var1/var2
'Print the original value Print rep '2.38095238
'Print the formatted number as a string 'Using() returns a string ... 'Then use Trim$() to remove the excess spaces 'left over from the formatting process, or remove 'the Trim$() function to keep the leading spaces. Print Trim$(Using("####.##", rep))
{:0)
Brandon Parker
|
|
|
Post by honky on Apr 10, 2021 14:42:07 GMT
Ok thank, so: No way "using" => string.
|
|
|
Post by tsh73 on Apr 10, 2021 17:12:08 GMT
May be USING just wrong function for that?
var1=500: var2=210 rep=var1/var2 print rep '2.38095238
repTruncated = ROUND(rep, 2) print repTruncated '-->2.38 ok
function ROUND(x, n) 'n=3 -> 12345.678? ROUND=int(x*10^n)/10^n end function
|
|
|
Post by B+ on Apr 10, 2021 17:15:49 GMT
Way, no strings attached! x = 2.38095238 print int(x * 100)/100
|
|
|
Post by B+ on Apr 10, 2021 17:29:43 GMT
tsh73 posted while I was editing my post, here is better ROUND
' using Steve's fix x = 2.5555 Print Round(x, 0) Print Round(x, 1) Print Round(x, 2) Print Round(x, 3) Print Round(x, 4)
Function Round (num, digits) ' steve's simplified Round = Int(num * 10 ^ digits + .5) / 10 ^ digits end function
edit: simpler version
|
|
|
Post by honky on Apr 10, 2021 17:55:48 GMT
"function ROUND(x, n)" it's nice ! And "print int(x * 100)/100" it's véry nice ! Thank you much, tsh73 and B+. The Champions
|
|
|
Post by tsh73 on Apr 10, 2021 18:55:48 GMT
Not better, just different It (as always) depends on what you really want
round to nearest (+0.5) or truncate (as visible in full number?)
3 2 2.6 2.5 2.56 2.55 2.556 2.555 2.5555 2.5555
x = 2.5555 Print Round(x, 0), ROUND(x, 0) Print Round(x, 1), ROUND(x, 1) Print Round(x, 2), ROUND(x, 2) Print Round(x, 3), ROUND(x, 3) Print Round(x, 4), ROUND(x, 4)
function Round (num, digits) ' steve's simplified Round = Int(num * 10 ^ digits + .5) / 10 ^ digits end function
function ROUND(x, n) 'n=3 -> 12345.678? ROUND=int(x*10^n)/10^n end function
|
|
|
Post by B+ on Apr 10, 2021 20:41:03 GMT
Better if you call something Round and it only truncates, surely.
|
|
|
Post by tsh73 on Apr 11, 2021 8:19:22 GMT
I don't really mind. Let version without rounding be named "trunc"
As a side note, I always thought using() just truncates. Then I found it rounds I went and checked some other basics. Indeed they *all* round.
(I really thought I knew it. Happened I knew it wrong.)
|
|
|
Post by honky on Apr 11, 2021 10:30:32 GMT
"trunc" suits me very well, I leave the crumbs to the mice.
|
|
|
Post by B+ on Apr 11, 2021 15:44:50 GMT
"trunc" suits me very well, I leave the crumbs to the mice. +1 Yes, and well named for that function.
|
|