division

Hi All,

sorry for a very stoopid newb question here.

I have this script that will simply calculate the percentage.
somehow it just doesn’t work I always get a 0. The only time it will work is if the dividend is equal the divisor.

I tried addition, subtraction and multiplication and works just fine, tried playing with the parenthesis too.

Just can’t get the division though. :frowning:

Here’s the script

var pointsEarned  = 0;
var wrongAnswer  = 0;
var rating  = 0.0;

var pointsEarnedResultGUI : GUIText;
var wrongAnwserResultGUI : GUIText;
 

function Update () 
{
	if(pointsEarned != 0)
	{
		rating = ((pointsEarned / 195)* (100));
		guiText.text = rating.ToString() +("%");
	}
}

Thanks,
Ray

You need to cast as floats.

With points earned = 0; (points earned / 195) is going to return an integer.

Use points earned = 0.00; (points earned / 195.00) instead.

Sweet,
Thanks Forrest.

feels good to understand what I’m trying to do.

I thought that by making the var rating a float will make the result a float.

:smile:

Ray

Well, I think it does… The problem is that the right side of the equation is performed (integer divided by an integer, resulting in an integer value of 0), then the result of that operation is cast in the data type of float (0 becomes 0.0).

I’m learning and understanding more about programming using unity and reading the community forum and asking question here. :smile:

thanks, really apppreciate it

Ray