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 68 views
1 answer 65 views
1 answer 73 views
4 answers 137 views
3 answers 319 views
2 answers 222 views
...