vikand
1
Hi,
I’m trying to round a variable to only two decimals without success. Anyone who can help me?
The LAST_VAR is a variable with a lot of decimals, which should be presented only with two decimals in the variable box.
luup.variable_set(VC_SID, VC_VAR2, LAST_VAR, 22)
Best regards,
Viktor
Depends whether you’re looking for a numeric or string result:
x = math.pi
x = x - x % 0.01
print (x)
-- or
print (string.format ("%.2f", math.pi))
Actually, these are truncations [Edit: ONE of these is… see correction in later post] - you may want to add 0.005 before truncating.
vikand
3
Thanks a lot, works fine!
Actually, I mis-spoke…
…the string.format version does round:
print (string.format ("%.2f", 123.456))
--> 123.46
However, the modulo ([tt]x - x % 0.01[/tt]) construction is, indeed, truncation.