Fixed point number to string - LUA Scripting - CHDK Forum  

Fixed point number to string

  • 3 Replies
  • 213 Views
Fixed point number to string
« on: 26 / March / 2019, 10:09:08 »
Advertisements
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"
Canon PowerShot SX220 1.01a / CHDK 1.4.1-4599

Re: Fixed point number to string
« Reply #1 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)

Ported :   A1200    SD940   G10    Powershot N    G16

Re: Fixed point number to string
« Reply #2 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))
Canon PowerShot SX220 1.01a / CHDK 1.4.1-4599

Re: Fixed point number to string
« Reply #3 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.
Ported :   A1200    SD940   G10    Powershot N    G16


 

Related Topics