Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Rounding up values

edited May 8 in Questions Posts: 6

So I have a code that gives almost whole numbers like 0.999827 or 7.00000234 and I have the numbers being displayed on the screen. I would like to round them up to whole numbers so that on the screen numbers like 1 and 7 appear, but how do I do that?

Comments

  • dave1707dave1707 Mod
    Posts: 5,744

    @DrSurname Do a forum or google search for string.format().

  • Posts: 21
    floor(yourNumber+0.5) will round up if over yourNumber >= 0.5 and down if < 0.5
  • Posts: 173

    @DrSurname As @GR00G0 said, if you want to round a number to the nearest whole number, do:

    function round(a)
        return math.floor(a+.5)
    end
    

    Another way to change the amount of values that appear is to use string.format(). For example, if you want to change 3.4567 to 3.45, then you only want two decimal places, so you do:

    string.format("%.02f",3.4567)
    
Sign In or Register to comment.