Format numbers

Just a simple question, can you format your numbers in codea? Eg. $1000 or 1,000, Thanks in advance

I could be wrong, but I don’t think you can. If it’s just to display you can always go with something like text(“$”…var,x,y)

What do you need this for?

string.format( s , number).
s is defined as for printf( s , number ) in C.
Look for examples on google

I’m trying to make a ‘simple’ game like Cookie clicker and I wanted to add commas in between the numbers so they are easier to read

Ok thanks Jmv38

@JonoGaming00 Try this.


displayMode(FULLSCREEN)

function setup()
    fontSize(40)
end

function draw()
    background(40,40,50)
    fill(255)
    text(addComma(123456.1234),WIDTH/2,550)
    text(addComma(123456789/4321),WIDTH/2,500)
    text(addComma(1234*5678),WIDTH/2,450)
    text(addComma(math.sqrt(1234567890)),WIDTH/2,400)
    text(addComma(1234^3),WIDTH/2,350)
    text(addComma(123456.1234),WIDTH/2,300)
    text(addComma("11223344556677889900.12345678"),WIDTH/2,250)
end

function addComma(value)
    loop=1
    while loop>0 do
        value,loop=string.gsub(value,"^(-?%d+)(%d%d%d)","%1,%2")
    end
    return value
end

From http://lua-users.org/wiki/FormattingNumbers

function comma_value(amount)
  local formatted = amount
  while true do  
    formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
    if (k==0) then
      break
    end
  end
  return formatted
end

Thanks a lot, both of those do what I want.

Sorry, another question. When the number is larger than a trillion, codea prints the number as (eg. 1.00183927e+14) is there a way to stop it dividing the numbers and displaying e?

@JonoGaming00 You can use string.format. Search google for all the options of string.format.

function setup()
    print(string.format("%.0f",1.00183927e+14))
end

Ok I will thanks again @dave1707