How to round a floating-point number to an integer in C#

1 Answer

0 votes
using System;

class Program
{
    static void Main()
    {
        float x = 9382.4F;
        long y = (long)Math.Round(x);
        Console.WriteLine(y);    
        
        x = 9382.5F;
        y = (long)Math.Round(x);
        Console.WriteLine(y);
        
        x = 9382.6F;
        y = (long)Math.Round(x);
        Console.WriteLine(y);
    }
}



/*
run:

9382
9382
9383

*/

 



answered May 14, 2025 by avibootz

Related questions

1 answer 47 views
1 answer 46 views
1 answer 58 views
4 answers 109 views
3 answers 285 views
2 answers 190 views
...