- Home /
Question by
BDX777 · Mar 17, 2015 at 02:54 AM ·
c#mathpercentagepercent
Calculating percentage in code is reversed?
Hi, I have a script here that calculates the percentage between two numbers, but it's in reverse. Reverse as in 0 is seen as 100% of 100 instead of the other way around. (100 being 100% of 100).
Does this have to be written in a specific way?
using UnityEngine;
using System.Collections;
public class percentagetest : MonoBehaviour
{
public float maxValue;
public float currentValue;
private float finalValue;
void Update ()
{
finalValue = (maxValue - currentValue) / maxValue * 100;
//Calculate percentage
}
void OnGUI ()
{
GUI.Label(new Rect(5,5,250,250),finalValue.ToString());
//Display result
}
}
Comment
Answer by Bunny83 · Mar 17, 2015 at 04:18 AM
Why it's the other way round? Because in this line:
finalValue = (maxValue - currentValue) / maxValue * 100;
your actually reverse it be subtracting the actual value from the max value. So it's reversed. What you probably want is this:
finalValue = currentValue / maxValue * 100;