- Home /
reducing a percentage value to desired decimal places
I have a gui label which is to display a percentage value. how can i reduce it to 1 or 2 decimal places?
GUI.Label(new Rect(50, 100, 50, 50), System.Convert.ToString(Calculate(ten)) + "%");
float Calculate(int card)
{
float total= (float)card/(float)deck*100;
return total;
}
Comment
You asked this question a few hours ago, now you ask it again. Why not just ask on your old question, or use the answers given to you? Downvote.
Answer by SirGive · Jan 11, 2012 at 06:54 PM
Check this question.
Any reason for using System.Convert.ToString()
instead of Calculate(ten).ToString()
?
Answer by dannyskim · Jan 11, 2012 at 06:57 PM
Using System;
Using System.Collections;
decimal Calculate( int card )
{
float total = (float)card/(float)deck*100;
decimal totalDecimal = (decimal)Math.Round( total, 2 );
return totalDecimal;
}
I think that should work, it's off the top of my head and may not compile, if so let me know and I'll try to fix it. In order to use decimal casting and regular Math.Round, you have to include the System namespace at the top.
Answer by yoyo · Jan 11, 2012 at 08:34 PM
GUI.Label(new Rect(50, 100, 50, 50), System.String.Format("{0:.00}", Calculate(ten)) + "%");