How to round up a double value to 3 decimal places in C#

3 Answers

0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Math.Ceiling(199.1831));
            Console.WriteLine(RoundUp(199.1831, 3));
        }
        public static double RoundUp(double d, int places)
        {
            double multiplier = Math.Pow(10, Convert.ToDouble(places));

            return Math.Ceiling(d * multiplier) / multiplier;
        }
    }
}

/*
run:
    
200
199.184
 
*/

 



answered Jan 17, 2017 by avibootz
0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Math.Ceiling(199.1831));
            Console.WriteLine(Math.Ceiling(199.1831 * 1000) / 1000);
        }
    }
}

/*
run:
     
200
199.184
  
*/

 



answered Jan 17, 2017 by avibootz
0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Math.Ceiling(199.1831));
            Console.WriteLine(0.001 * Math.Ceiling(1000 * 199.1831));
        }
    }
}

/*
run:
     
200
199.184
  
*/

 



answered Jan 17, 2017 by avibootz
edited Jan 17, 2017 by avibootz

Related questions

3 answers 300 views
3 answers 211 views
1 answer 172 views
1 answer 140 views
140 views asked Jan 17, 2017 by avibootz
1 answer 154 views
1 answer 257 views
...