This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
willsor's profile picture

[Lua][Question] Limiting decimal places of a variable, after a calculation

Started by willsor, 17 July 2012 - 05:58 PM
willsor #1
Posted 17 July 2012 - 07:58 PM
Hi
I am making a cobblestone generator to create EMC trash, and I have it mostly working.
It has a variable "a" which is how much cobblestone has been made (1 cobble has an EMC of 1)
Also, a variable "f" which is how many diamonds have been made.
Also a fixed variable "d" = 8192 which is the EMC value of diamond

I have the display showing the raw EMC value, and the amount of diamonds where f = a / d

so if exactly 8192 cobble has been made, it will calculate 8192 / 8192 = 1 diamond
It works fine, but currently has far too many sig figs
it shows it as, for example, 1.4468758765 diamonds.

how can I limit this to only show the number to only 1 decimal place, like 1.4 for the example above.

I'm not too worried about the rounding of the 2nd decimal place if that takes extra coding

thanks very much, hope that wasn't too confusing :P/>/>
MysticT #2
Posted 17 July 2012 - 08:29 PM
You could floor the number to an integer (no decimals) using math.floor. But if you want to have decimals, the only way I can think now is formating the string:

print(string.format("%.1f", f))
It formats the number into a string, the .1 means to leave 1 decimal only.
willsor #3
Posted 18 July 2012 - 09:46 AM
Thanks, MysticT, got it to work!