Author Topic: String functions  (Read 2325 times)

garry23

  • Contributor
  • Hero Member
  • *****
  • Posts: 2218
String functions
« 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

Code: [Select]
string.format("%.2f", num)

a1ex

  • Administrator
  • Hero Member
  • *****
  • Posts: 12564
Re: String functions
« Reply #1 on: November 29, 2018, 12:03:02 AM »
Confirmed. Oddly enough, using %s for floating-point numbers appears to work.

garry23

  • Contributor
  • Hero Member
  • *****
  • Posts: 2218
Re: String functions
« Reply #2 on: November 29, 2018, 12:12:23 AM »
@a1ex

Thanks for the confirmation and the pointer to %s

garry23

  • Contributor
  • Hero Member
  • *****
  • Posts: 2218
Re: String functions
« Reply #3 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:

Code: [Select]
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