String functions

Started by garry23, November 28, 2018, 11:51:21 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

garry23

@a1ex

I've concluded that not all the string functions are in Lua. Am I right?

The following simply doesn't work

string.format("%.2f", num)

a1ex

Confirmed. Oddly enough, using %s for floating-point numbers appears to work.

garry23

@a1ex

Thanks for the confirmation and the pointer to %s

garry23

@a1ex

I also guess using %s wouldn't allow you to control the decimal place formatting.

Not having %.xf is not an issue as I can achieve the result other ways. For instance in my DoF Bar I do this:

function myround(num,dp)
    -- num = a number, dp = decimal places required to show
    -- returns number as a string
    if dp == 0 then
        num = tostring(math.floor(num))
    else
        --text =
        num = tostring(math.floor(num)).."."..string.sub(tostring(math.floor(num*10^dp)),-dp,-1)
    end
    return num
end