Math.Round() function doesn't appear to be working :|

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RiK ooo
    New Member
    • May 2007
    • 2

    Math.Round() function doesn't appear to be working :|

    Hi, i'm currently working on C# project and I want to make use of the Math.Round() function to display only the first to digits of a float value. Unfortunately this doesn't seem to be working for me. I haven't much experience in using the Timer class or the Math class. I may be overlooking something stupid and the solution is probably really simple (happens a lot to me).. here's the piece of code..

    Code:
    //Retrieve time and round it off.
                float test = 5.123f;
                _t += _tempTimer.Interval;
                Math.Round(test, 2);
    
                //Draw game time text.
    
                e.Graphics.DrawString("Secs: " + test,
                    new Font(new FontFamily("Lucida Console"), 8.0f),
                    new SolidBrush(Color.White), new PointF(300, 10));
    ..using the above, the "test" or the "_t" values do not round off. Anybody have any ideas on why? Thanks in advance.
    Last edited by Niheel; Dec 21 '10, 06:16 PM.
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    Rounds a value to the nearest integer or to the specified number of fractional digits.


    Math.Round returns the rounded value, it doesn't change the value of the variable it is supplied with.

    You can either assign the rounded value to temporary storage, or just use it in your DrawString method :)

    Comment

    Working...