3

I am using lua 5.3.2 and the following piece of code gives me an error:

string.format("%d", 1.16 * 100)

whereas the following line works fine

string.format("%d", 1.25 * 100)

This is probably related to this question but the failure depends on the floating point value. Given that, in my case, a local variable (v) holds the float value, and is generated by an expresson that produces a value between 0 and 2 rounded to 2 decimal places.

How can I modify the code to ensure this doesn't fail for any possible value of v ?

Community
  • 1
  • 1
koushik
  • 186
  • 2
  • 4

1 Answers1

4

You can use math.floor to convert to integer and add +0.5 if you need to round it: math.floor(1.16 * 100 + 0.5). Alternatively, "%.0f" should have the desired effect as well.

Paul Kulchenko
  • 25,884
  • 3
  • 38
  • 56