none
How to round off a float to 2 decimal places?

    Question

  • Hello,

    float f = 1.345698;

    I want f to be 1.34. Is there any C# methods like Math.Round that I can use? I tried Math.Round but it only accepts decimals and when I try to cast my float as a decimal I get an error saying the number is to large to cast as a decimal.

    Thanks,
    Rhonika


    Friday, December 18, 2009 8:04 PM

Answers

  • If you want it to be 1.34, you'll need to truncate, not round, the value:

        float f = 1.345698F;
    
        float truncated = (float)(Math.Truncate((double)f*100.0) / 100.0);
    
        float rounded = (float)(Math.Round((double)f, 2);
    

    If you need a way to truncate to a specified number of digits, for floats, that's more general purpose, you could move this into an extension method:

    public static float Truncate(this float value, int digits)
    {
        double mult = Math.Pow(10.0, digits);
        double result = Math.Truncate( mult * value ) / mult;
        return (float) result;
    }
    Then you could just do:

        float truncated = f.Truncate(2);

    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by rrhonika Friday, December 18, 2009 8:39 PM
    Friday, December 18, 2009 8:21 PM

All replies

  • namespace ForumHelp
    {
        using System;
    
        class Program
        {
            static void Main(string[] args)
            {
                float foo = 3.14516f;
                decimal bar = Convert.ToDecimal(foo);
                bar = Math.Round(bar, 2);
                Console.WriteLine("Unrounded: {0}\nRounded {1}", foo, bar);
                Console.ReadLine();
            }
        }
    }
    


    Blog (WPF/C#)
    Crystal (WPF Google Contact UI/C# Library)
    Friday, December 18, 2009 8:21 PM
  • If you want it to be 1.34, you'll need to truncate, not round, the value:

        float f = 1.345698F;
    
        float truncated = (float)(Math.Truncate((double)f*100.0) / 100.0);
    
        float rounded = (float)(Math.Round((double)f, 2);
    

    If you need a way to truncate to a specified number of digits, for floats, that's more general purpose, you could move this into an extension method:

    public static float Truncate(this float value, int digits)
    {
        double mult = Math.Pow(10.0, digits);
        double result = Math.Truncate( mult * value ) / mult;
        return (float) result;
    }
    Then you could just do:

        float truncated = f.Truncate(2);

    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by rrhonika Friday, December 18, 2009 8:39 PM
    Friday, December 18, 2009 8:21 PM
  • float mynum = 1.345698f;
    double r = Math.Floor(mynum * 100) / 100;
    Friday, December 18, 2009 8:25 PM
  • Thanks!!
    Friday, December 18, 2009 8:39 PM
  • Rhonika,

    You got plenty of options already specified but I wanted to throw this out.  If you want to keep the precision in the variable and simply change what is displayed you could use string.Format like so:

                float x = 1.345698f;
                string msg = String.Format("{0:0.00}", x);
    

    Of course, this doesn't account for 1.34 instead of 1.35 which I will assume is a typo or that you meant to ask for truncation which has also been answered.

    Michael Fischer

    Friday, December 18, 2009 8:42 PM
  • So I am just trying something like this today, and the solution presented here has a huge gaping problem...A float converted to a double is not always the same value, at least not in VS2010 in C#.

    Given this code:

            

    privatestringValidateContributionRate()         {             string result = null;             var truncated = (float) (Math.Truncate(ContributionRate*10000.0)/10000.0);             if ((ContributionRate - truncated) > 0)             {                 result = "Level " + Name + ": Contribution Rate can only be 4 decimal places";              }

    It turns out that 0.12f becomes a double value of 0.11999999731779099.

    Multiply that by 10000 and divide again, you get .1199, instead of the expected .12

    I don't suppose anyone has a solution to this conundrum?

      

    Thursday, March 06, 2014 7:41 PM
  • Start a new thread this one is 4+years old. (and besides that the post marked as Answer is wrong - there is no such number as 1.34 in float or double, just the same as there is no such number as a decimal representation of 1/7)

    Paul Linton

    Thursday, March 06, 2014 9:17 PM