How to convert a specified value to a 64-bit signed integer (Convert.ToInt64) in C#

1 Answer

0 votes
using System;

public class Program
{
    public static void Main(string[] args)
    {
        float[] arr = { Single.MinValue, -2.18e10f, -4012.309f, -18.07f,
                        0f, 7.018e-16f, 981.276f, 23004.7815f, Single.MaxValue };
        
        foreach (float value in arr) {
            try {
                long result = Convert.ToInt64(value);
                Console.WriteLine("{0} = {1}", value, result);
            }
            catch (OverflowException) {
                Console.WriteLine("{0} is outside the range of the Int64 type", value);
            }
        }
    }
}



/*
run:

-3.402823E+38 is outside the range of the Int64 type
-2.18E+10 = -21799999488
-4012.309 = -4012
-18.07 = -18
0 = 0
7.018E-16 = 0
981.276 = 981
23004.78 = 23005
3.402823E+38 is outside the range of the Int64 type

*/

 



answered May 24, 2024 by avibootz
...