How to round a number to a multiple of 5 in C#

1 Answer

0 votes
using System;
 
public class Program
{
    public static double roundToMultipleOf(double number, double multipleOf) {
        return multipleOf * (long)Math.Round(number / multipleOf, MidpointRounding.AwayFromZero);
    }
 
    public static void Main(string[] args)
    {
        Console.WriteLine(roundToMultipleOf(9, 5));
        Console.WriteLine(roundToMultipleOf(19, 5));
        Console.WriteLine(roundToMultipleOf(71, 5));
    }
}
 
  
  
  
/*
run:
     
10
20
70
     
*/

 



answered Jun 10, 2024 by avibootz

Related questions

1 answer 94 views
1 answer 84 views
1 answer 82 views
1 answer 93 views
2 answers 118 views
1 answer 111 views
1 answer 107 views
...