Magic Lantern Forum

Developing Magic Lantern => Scripting Corner => Scripting Q&A => Topic started by: garry23 on November 28, 2018, 11:51:21 PM

Title: String functions
Post by: garry23 on November 28, 2018, 11:51:21 PM
@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)
Title: Re: String functions
Post by: a1ex on November 29, 2018, 12:03:02 AM
Confirmed. Oddly enough, using %s for floating-point numbers appears to work.
Title: Re: String functions
Post by: garry23 on November 29, 2018, 12:12:23 AM
@a1ex

Thanks for the confirmation and the pointer to %s
Title: Re: String functions
Post by: garry23 on November 29, 2018, 08:24:14 AM
@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