Howdy, Stranger!

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

Rounding for Sugarcube?

I have a system that does some math to determine a base value, and then adds a random value and subtracts a random value.

A few of the results have been 10.2, 8.5, 6.8 and 11.049999999999999

Is there a piece of code I can put in there that stops these decimals from appearing? Can I have the system round up or down if the value is not a whole integer? Can I change 10.2 to just 10 and 6.8 to just 7, and most importantly can I get rid of that incredibly long series of 499999s?

Comments

  • edited May 2015
    It would help to know how you’re generating the random number. For instance, the following code returns a random integer (i.e., whole number; not decimal) between a range of 0 and 10:
    var randomNumber=Math.floor(Math.random()*11)
    
    So, if you’re using JS to generate the number, then this will work.
  • timsamoff wrote: »
    It would help to know how you’re generating the random number. For instance, the following code returns a random integer (i.e., whole number; not decimal) between a range of 0 and 10:
    var randomNumber=Math.floor(Math.random()*11)
    
    So, if you’re using JS to generate the number, then this will work.

    I'm using the random(1, variable) method.

    <<set $total = $base + $random1 - $random2>>

    The $base is not static and can change based on what the player does.

    <<set $aftermath = $total/ $math3>> is the sum I want to calculate without decimals.

    $math3 is determined by
    <<set $math0 = 5>> 5 or any other number really
    <<set $math1 = ($math0 * 5) - 10>>
    <<set $math2 = 100 - $math1>>
    <<set $math3 = 100 / $math2>>

    So you can see how the total might also be decimal, but that's fine because the player doesn't see this bit. However, the player does see $total / $math3 = $aftermath, and that is the number I want to not have decimals.

    A little complicated, but is there some way I can format this so that if an integer is like 10.5 it goes up to 11, but if its 10.2 it goes down to just 10?
  • <<set $aftermath = Math.round($total / $math3)>>
    
  • <<set $aftermath = Math.round($total / $math3)>>
    

    This answers my question. I think I accidentally set this as a discussion rather than a question, but still, thanks. All of my values are now whole integers.
Sign In or Register to comment.