CHDK Forum

Using CHDK => Script Writing => LUA Scripting => Topic started by: artem78 on 26 / March / 2019, 10:09:08

Title: Fixed point number to string
Post by: artem78 on 26 / March / 2019, 10:09:08
Hello.
I`m newbie in lua and chdk scripting. How can I insert variable with fixed point in string using string.format()? Unfortunately %f not work.

Code: [Select]
local x = 3140 --3'140
local s = string.format('x value is %f', x) --I need to get: "x value is 3.140"
Title: Re: Fixed point number to string
Post by: waterwingz on 26 / March / 2019, 10:33:45
How can I insert variable with fixed point in string using string.format()?
I can't test it at the moment, but I think this will work :
Code: [Select]
local x = 3140 --3'140
local s = string.format('x value is %01d.%03d', x/1000,x%1000)

Title: Re: Fixed point number to string
Post by: artem78 on 26 / March / 2019, 10:56:05
Quote
but I think this will work

This code almost fine, but has a mistake: with negative numbers minus sign is present both in the whole and in the fractional part. So I added math.abs to fraction part:
Code: [Select]
local s = string.format('x value is %01d.%03d', x/1000,math.abs(x%1000))
Title: Re: Fixed point number to string
Post by: waterwingz on 26 / March / 2019, 13:01:28
This code almost fine, but has a mistake:
Probably only a mistake if you specify signed fixed point numbers.