How to display a float to 2 decimal places in C#

4 Answers

0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            float f = 0.314f;

            Console.WriteLine(f.ToString("0.00"));
        }
    }
}

/*
run:

0.31
 
*/

 



answered Jun 4, 2015 by avibootz
edited Feb 18, 2016 by avibootz
0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            float f = 0.3554f;

            Console.WriteLine(f.ToString("0.00"));
        }
    }
}

/*
run:

0.36
 
*/

 



answered Jun 4, 2015 by avibootz
edited Feb 18, 2016 by avibootz
0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            float f = 0.314f;

            Console.WriteLine(f.ToString("n2"));
        }
    }
}

/*
run:

0.31
 
*/

 



answered Jun 4, 2015 by avibootz
edited Feb 18, 2016 by avibootz
0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            float f = 0.314f;

            Console.WriteLine(string.Format("{0:0.00}", f));
        }
    }
}

/*
run:

0.31
 
*/

 



answered Jun 4, 2015 by avibootz
edited Feb 18, 2016 by avibootz

Related questions

4 answers 426 views
2 answers 365 views
4 answers 537 views
2 answers 394 views
1 answer 113 views
2 answers 236 views
1 answer 199 views
...