Problem -
I wanted a function to strip decimal places from a float but to absolutely NOT round (either up or down) the result and just remove the unwanted decimal places. Basically 'truncate' the number of decimal places of a float without altering the original format or values of the individual decimal places.
I came up with various solutions but this is my choice to use.
The detailed explanation is longer than the simple function
Solution -
Code: Select all
-- A function to return a limited number of decimal places WITHOUT !!! rounding
-- an example by Joe Hanley (JPH) 2022
function stripDecimalPlaces(num, numDecimalPlaces)
local factor = 10^(numDecimalPlaces)
return math.floor(num * factor) / factor
end
-- ...........................................................................................................................
-- Thats it. The above is the complete funtion.
-- Here are some examples of appropriate function calls.
-- This returns 1.23 for the number 1.2311 with 2 decimal places
local nonRoundedFormat = stripDecimalPlaces(1.23111, 2)
print (nonRoundedFormat)
-- This returns 1.23 for the number 1.23999 with 2 decimal places
local nonRoundedFormat = stripDecimalPlaces(1.23999, 2)
print (nonRoundedFormat)
--[[
How does it operate ?
Ok. .....
The function takes a number and a desired number of decimal places as input and returns a new number with the same value as the original
number, but with a limited number of decimal places. The function does this by first multiplying the input number by 10 raised to the power
of the desired number of decimal places. This has the effect of shifting the decimal point to the right by the desired number of places.
For example, if the input number is 1.23111 and the desired number of decimal places is 2,
the result of 1.23111 * 10^2 is 123.111
Next, the function takes the result of this multiplication and applies the math.floor function to it.
This function rounds the number down to the nearest integer. So, in the example above, the result of math.floor(123.111) would be 123.
Finally, the function divides the result of the math.floor function by the same factor that was used in the first step
10 raised to the power of the desired number of decimal places). This has the effect of shifting the decimal point back to its original position,
but with a limited number of decimal places. So, in the example above, the final result of the function would be 1.23.
The function does not perform any rounding, so it will simply truncate any decimal places beyond the desired number of decimal places.
For example, if the input number is 1.23999 and the desired number of decimal places is 2, the function will return 1.23,
rather than rounding the number up to 1.24.
The function is called with two arguments: the input number and the desired number of decimal places.
The function first defines a local variable factor which is equal to 10 raised to the power of the desired number of decimal places.
It then returns the result of the input number multiplied by factor, passed through the math.floor function, and then divided by factor again.
The final result is the input number with a limited number of decimal places, without any rounding. --]]
Joe