Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,919 questions

51,852 answers

573 users

How to convert long to int in C#

3 Answers

0 votes
using System;

class Program
{
    static void Main() {
        long l = 89284;
        int n = Convert.ToInt32(l);

        Console.Write(n);
    }
}



/*
run:

89284

*/

 



answered Feb 10, 2021 by avibootz
0 votes
using System;

public class Program
{
    public static void Main(string[] args)
    {
        long bigNumber = 123456701;
        int intNumber = (int)bigNumber; // May cause overflow
        
        Console.WriteLine(intNumber);
    }
}



/*
run:

123456701

*/

 



answered May 18, 2025 by avibootz
0 votes
using System;

public class Program
{
    public static void Main(string[] args)
    {
        long bigNumber = 123456701346346;
        int intNumber = 0;
        // Safe Conversion Check
        if (bigNumber >= int.MinValue && bigNumber <= int.MaxValue) {
            intNumber = (int) bigNumber;
        } else {
            Console.WriteLine("Overflow detected!");
        }
        
        Console.WriteLine(intNumber);
    }
}



/*
run:

Overflow detected!
0

*/

 



answered May 18, 2025 by avibootz

Related questions

1 answer 55 views
1 answer 85 views
1 answer 109 views
109 views asked Aug 5, 2019 by avibootz
2 answers 100 views
1 answer 288 views
288 views asked Feb 12, 2021 by avibootz
1 answer 142 views
142 views asked Feb 12, 2021 by avibootz
...