2

I need to find out a way to format large numbers as strings without transforming them into other numbers.

My code is here:

local no = 91234573457652910
print(no)                       -- prints  9.1234573457653e+16
print(tostring(no))             -- prints  9.1234573457653e+16
print(string.format("%.f", no)) -- prints  91234573457652912
print(string.format("%d", no))  -- prints  91234573457652912

As you can see Lua transformed them into the other numbers but I want to print the numbers as it is.

I want to know a way to print the numbers as it is in the output. So is there any solution for this issue?

  • 2
    Does this answer your question? [Dealing with big numbers in Lua](https://stackoverflow.com/questions/45270543/dealing-with-big-numbers-in-lua) – Ivo Aug 07 '23 at 11:30
  • 1
    Lua 5.3+ prints 91234573457652910 in all cases, except "%.f" because this number does not fit the mantissa of double precision floats. – lhf Aug 07 '23 at 11:44

1 Answers1

0

if a number exceeds the lua math.maxinteger constant. you lose precision when doing anything with it. but there are modules/libraries you can find that can help you with this.

Kujo Ludin
  • 35
  • 4
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 16 '23 at 11:34