Reputation: 903
In lua, how can I format a number to have 2 decimal places? I have a value example: 25.333 and I want it to display 25.33 instead. Sometimes it's an integer like 55 and I want it to display 55.00 instead. This is for displaying currency values. I've tried example formatting functions, and none do what I want. Any advice is appreciated. Jerry
Upvotes: 1
Views: 5508
Reputation:
See this answer. Basically, you'd want something like:
rdahlgren@deimos: pts/1: 5 files 48Kb$ lua
Lua 5.2.0 Copyright (C) 1994-2011 Lua.org, PUC-Rio
> x = 25
> y = 25.3333
> print(string.format("%.2f", x))
25.00
> print(string.format("%.2f", y))
25.33
>
Upvotes: 7