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

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

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

Disclosure: My content contains affiliate links.

43,567 questions

56,785 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 151 views
1 answer 192 views
192 views asked Mar 17, 2025 by avibootz
1 answer 226 views
226 views asked Aug 5, 2019 by avibootz
2 answers 240 views
1 answer 402 views
402 views asked Feb 12, 2021 by avibootz
...