Removing Trailing Zeros

Hello all,

I feel sheepish to keep asking for help like this.

But, I can’t seem to figure this one out. I am making an app that outputs numbers. Sometimes those numbers are decimals.

I have a for loop that formats all the answers to a user specified amount of decimal places. The maximum allowed decimals is 5 and the minimum is 2. 

Sometimes the answers come out like 45.00000 Is there any way to utilize string.format() or any other code to check if the last char in a string is a 0 or . and if it is remove it?

I’m not sure who you are ending up with trailing zeros. I fix my numbers to X decimal places like so:

local myNumber = 12.3456789

local numDecimals = 2

local myNewNumber = math.round(myNumber * math.pow(10, numDecimals)) / math.pow(10, numDecimals)

print(myNewNumber) --12.34

and I don’t ever seem to get trailing zeros.

I’m not sure who you are ending up with trailing zeros. I fix my numbers to X decimal places like so:

local myNumber = 12.3456789

local numDecimals = 2

local myNewNumber = math.round(myNumber * math.pow(10, numDecimals)) / math.pow(10, numDecimals)

print(myNewNumber) --12.34

and I don’t ever seem to get trailing zeros.