Disappearing Zeros

Is there any way to stop extra zeros from being dropped when changing a decimal number string into a true number?

EG:

If I go tonumber(“5.00”) it results in just 5

A function of my app is for the user to create a number 1 digit at a time (Like a calculator). I’m using the Fontmanager library to make bitmap fonts (for WP8) instead of a straight string. But part of the function of that library is to turn any number strings into true numbers with tonumber().

So when I have someone trying to make up the number 5.004 for example, everytime they get to the 5.0 part it just keeps changing it to 5

Look at string.format()

local stringWithZeros = string.format("%0.2f", number)

print(stringWithZeros)

Rob

Hi Rob, unfortunately that doesn’t seem to work. All that does is force all number to now have 2 decimal places of zeros. What I need is to stop having whole numbers turned into whole numbers so to speak. They can have as many zeros as they want.

EG: The user puts in 5.000005607

While they are punching in those numbers 1 at a time it keeps turning the “whole” number into a whole number. 5.0 immediately turns into 5

I see.  Can you maintain your display as a string of digit characters and not convert it to a number?   Or you can still use string format, though it will require you to keep track of the length you need:

local len = 3

local numToPrint = string.format("%0." … len … “f”, num)

As you keep adding 0’s after the period, keep incrementing len…  thought it will be easier to just take the input text and keep it text.

Rob

Look at string.format()

local stringWithZeros = string.format("%0.2f", number)

print(stringWithZeros)

Rob

Hi Rob, unfortunately that doesn’t seem to work. All that does is force all number to now have 2 decimal places of zeros. What I need is to stop having whole numbers turned into whole numbers so to speak. They can have as many zeros as they want.

EG: The user puts in 5.000005607

While they are punching in those numbers 1 at a time it keeps turning the “whole” number into a whole number. 5.0 immediately turns into 5

I see.  Can you maintain your display as a string of digit characters and not convert it to a number?   Or you can still use string format, though it will require you to keep track of the length you need:

local len = 3

local numToPrint = string.format("%0." … len … “f”, num)

As you keep adding 0’s after the period, keep incrementing len…  thought it will be easier to just take the input text and keep it text.

Rob