Shailesh Jaiswal
Shailesh Jaiswal

Reputation: 3654

How to convert long to int in .net?

I am developing window phone 7 application in silverlight. I am new to the window phone 7 application. I have the long value in String format as follows

String Am = AmountTextBox.Text.ToString() 

The AmountTextBox.Text.ToString() in the above code is long value which is in string format. I want to store a 15 digit inter value in my application.

I found the following link for conversion.

Can I convert long to int?

How should I convert a long value which is in string format to int ? Can you please provide me any code or link through which I can resolve the above issue ? If I am doing anything wrong then please guide me.

Upvotes: 31

Views: 135099

Answers (7)

Matt
Matt

Reputation: 901

In C# int has 32 bits and long has 64 bits. Therefore not all values of long can be int. Maybe get an array of ints?

public static int[] ToIntArray(long l)
{
    var longBytes = BitConverter.GetBytes(l);

    // Get integers from the first and the last 4 bytes of long
    return new int[] {
        BitConverter.ToInt32(longBytes, 0),
        BitConverter.ToInt32(longBytes, 4)
    };
}

Upvotes: -1

Shravani
Shravani

Reputation: 11

You can convert the input long to Object and then convert it to int32.

Object a = e;
int w = Convert.ToInt32(a); 

It worked for me.

Upvotes: 1

sloth
sloth

Reputation: 101072

You can't store a 15 digit integer since the maximum value for an integer is 2,147,483,647.

What's wrong with a long-Value?

You could use TryParse() to get the long-Value from yout user input:

String Am = AmountTextBox.Text.ToString();
long l;
Int64.TryParse(Am, out l);

It will return false if the text can't be converted to long, so it's pretty safe to use.

Otherwise, converting a long to int is a easy as

int i = (int)yourLongValue;

if you're happy with discarding MSBs and taking LSBs.

Upvotes: 31

Cody Gray
Cody Gray

Reputation: 244752

You have a number stored as a string, and you want to convert it to a numeric type.

You can't convert it to type int (also known as Int32), because as the other answers have mentioned, that type does not have sufficient range to store your intended value.

Instead, you need to convert the value to a long (also known as Int64), instead. The simplest and most painless way to do that is using the TryParse method, which converts a string representation of a number to its 64-bit signed integer equivalent.

The advantage of using this method (instead of something like Parse) is that it does not throw an exception if the conversion fails. Since exceptions are expensive, you should avoid throwing them unless absolutely necessary. Instead, you specify the string containing the number to convert as the first argument to the method, and an out value to receive the converted number if the conversion succeeds. The return value is a Boolean, indicating whether or not the conversion was successful.

Sample code:

private void ConvertNumber(string value)
{
    Int64 number; // receives the converted numeric value, if conversion succeeds

    bool result = Int64.TryParse(value, out number);
    if (result)
    {
         // The return value was True, so the conversion was successful
         Console.WriteLine("Converted '{0}' to {1}.", value, number);
    }
    else
    {
        // Make sure the string object is not null, for display purposes
        if (value == null)
        {
            value = String.Empty;
        }

         // The return value was False, so the conversion failed
        Console.WriteLine("Attempted conversion of '{0}' failed.", value);
    }
}

Upvotes: 2

Ian
Ian

Reputation: 34489

Use Convert.ToInt32(). If the value is too big for an int then it will throw an OverflowException.

This method can take a whole range of values, including Int64 and Strings.

Upvotes: 49

cusimar9
cusimar9

Reputation: 5259

A Long value stores more data than an Integer 32 value, so if you want to keep all 15 digits you can't convert to a standard Integer.

But aside from 'which' datatype you want to use, the Convert class should help you i.e. Convert.ToInt64 which may do what you want

Upvotes: 1

GavinH
GavinH

Reputation: 2213

The maximum Int32 value in C# is 2,147,483,647, shorter than the 15 digits you require.

Are you sure you need to convert this value to an int? It sounds like you'd be better off storing the value in a long, or leaving it as a String.

Upvotes: 1

Related Questions