- Home /
How can I declare a variable as the 100% of another variable?
How can I declare a variable as the 100% of another variable?
Example:
var amount : int = 30; var percent : int = 100;
Percent variable must be the same as the amount variable.
Example:
percent = percent / 2; (30:2=15, so percent must be 15 now)
I don't get this question, sorry... Your example doesn't explain what you want to do. Dividing 30 by 2 gives 15, yes .... So what do you actually want? o.O'
So you want a variable to be a percentage of another variable, and the percentage value is a range from 0-100?
The new number will be = amount*(percent/100)
So in your case: 30*(100/100) = 30, which is 100% of your amount(30)
Bunny83 is right, kind of weird wording....
It make be easier to understand if you store your 'percentage' value as a float or something from 0-1 and you can just do the multiplication rather than dividing it every time
Are you looking for a pointer? I don't about the js syntax though.
in C# you could use a property. eg:
public class ExampleClass : $$anonymous$$onoBehaviour
{
public const int fullAmount = 30;
private float m_amount;
public float Amount
{
get { return m_amount; }
set
{
m_amount = value;
Percent = (m_amount / fullAmount) * 100; // "* 100" to get to your units
}
}
private float m_percent;
public float Percent
{
get { return m_percent; }
set
{
m_percent = value;
Amount = fullAmount * m_percent / 100;
}
}
}
That way when you change either one, the other will update to the correct relative value. You could also add conditions in the Percent's "set' function if you want to limit the percentage from 0 to 100 or something like that.
I'm not sure whether JS has an equivalent to a property, but if not you could just have a SetPercent() function and a GetPercent() function, etc. C# just does it more aesthetically.
@liamcary - you've got an infinite recursion in that code...
I think you meant:
public class ExampleClass : $$anonymous$$onoBehaviour
{
public const int fullAmount = 30;
private float m_amount;
public float Amount
{
get { return m_amount; }
set
{
m_amount = value;
m_percent = (m_amount / fullAmount) * 100; // "* 100" to get to your units
}
}
private float m_percent;
public float Percent
{
get { return m_percent; }
set
{
m_percent = value;
m_amount = fullAmount * m_percent / 100;
}
}
}
Your answer
Follow this Question
Related Questions
convert variables to C#? 1 Answer
Redim a variable 3 Answers
C# Custom Class - Declare Variable's Value 1 Answer
C#: Declaring a script as a variable 1 Answer