How to round a number up to the nearest 10 in C#

1 Answer

0 votes
using System;
  
class Program
{
    static int roundUpToNearest10(double num) {
        return (int)Math.Ceiling(num / 10) * 10;
    }
    static void Main() {
        Console.WriteLine(roundUpToNearest10(33));
        Console.WriteLine(roundUpToNearest10(59));
        Console.WriteLine(roundUpToNearest10(599.99));
        Console.WriteLine(roundUpToNearest10(3.14));
        Console.WriteLine(roundUpToNearest10(2));
        Console.WriteLine(roundUpToNearest10(19));
        Console.WriteLine(roundUpToNearest10(-12));
        Console.WriteLine(roundUpToNearest10(-101));
        Console.WriteLine(roundUpToNearest10(-109));
    }
}
  
  
  
  
/*
run:
   
40
60
600
10
10
20
-10
-100
-100
   
*/

 



answered Jun 10, 2022 by avibootz
edited Jun 10, 2022 by avibootz

Related questions

1 answer 121 views
1 answer 119 views
1 answer 119 views
1 answer 125 views
1 answer 109 views
1 answer 96 views
1 answer 114 views
...