Howdy, Stranger!

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

How to express a variable as a percentage?

I use Sugarcube.

Imagine a space game where you have an oxygen tank. And let's say, every passage, the $air variable goes down by 1, and $maxAir, the full-capacity of the oxygen tank, is set at 20.

But you can collect tools to upgrade $maxAir, meaning you can stay outside for longer periods of time before having to refill your oxygen tank. Let's say you can upgrade $maxAir to 50, 75 and then 100.

Is there a single bit of code you can do to code the $maxAir variable on a scale of 0-100, where 100% is $maxAir and the percentage displayed is whatever $air is relative to $maxAir? Or would I have to code each upgrade differently?

Like 10 $air with a $maxAir of 20 would display 50%, but 10 $air with a $maxAir of 50 would display as 20%

Any help is appreciated.

Comments

  • I learnt maths when dinosaurs still roamed the earth and tablets were made of stone but I seem to remember that: percent = (value / maxValue) x 100

    So try:
    <<set $percent to (($air / $maxAir) * 100)>>
    
  • greyelf wrote: »
    I learnt maths when dinosaurs still roamed the earth and tablets were made of stone but I seem to remember that: percent = (value / maxValue) x 100

    So try:
    <<set $percent to (($air / $maxAir) * 100)>>
    

    As usual, I was trying to gerrymander some overly complicated and far more inefficient method on my own when the answer was this simple all along.

    Thank you.
Sign In or Register to comment.