126

How could i convert data from string to long in C#?

I have data

String strValue[i] ="1100.25";

now i want it in

long l1;
Raktim Biswas
  • 3,891
  • 5
  • 25
  • 31
MayureshP
  • 2,364
  • 6
  • 30
  • 39
  • 1
    Longs represent integers. You've given a non-integer. What would you want to do with that? – Jon Skeet Jun 13 '11 at 12:21
  • 1
    What if strValue is "1100.75" ? See: http://stackoverflow.com/questions/633335/how-might-i-convert-a-double-to-the-nearest-integer-value – magma Jun 13 '11 at 12:22
  • @magma: if you convert it into decimal then it will round off the number, i.e in this case if given number was 1100.75 then it will output 1101. – love Computer science Jun 13 '11 at 12:47
  • 1
    @charlie I was merely raising the issue, because MayP did not appear to be fully aware of the implication here. He might want to simply get rid of the non-integer part without any rounding, and that's fine, provided that it's really what he wants. Thank you though. – magma Jun 13 '11 at 13:19

8 Answers8

51

If you want to get the integer part of that number you must first convert it to a floating number then cast to long.

long l1 = (long)Convert.ToDouble("1100.25");

You can use Math class to round up the number as you like, or just truncate...

BrunoLM
  • 94,090
  • 80
  • 289
  • 441
36

You can also use long.TryParse and long.Parse.

long l1;
l1 = long.Parse("1100.25");
//or
long.TryParse("1100.25", out l1);
majid zareei
  • 523
  • 6
  • 9
17

http://msdn.microsoft.com/en-us/library/system.convert.aspx

l1 = Convert.ToInt64(strValue)

Though the example you gave isn't an integer, so I'm not sure why you want it as a long.

John
  • 2,325
  • 14
  • 21
8
long l1 = Convert.ToInt64(strValue);

That should do it.

Pang
  • 9,073
  • 146
  • 84
  • 117
stuartmclark
  • 1,163
  • 12
  • 21
6

You won't be able to convert it directly to long because of the decimal point i think you should convert it into decimal and then convert it into long something like this:

String strValue[i] = "1100.25";
long l1 = Convert.ToInt64(Convert.ToDecimal(strValue));

hope this helps!

love Computer science
  • 1,768
  • 3
  • 19
  • 39
3

long is internally represented as System.Int64 which is a 64-bit signed integer. The value you have taken "1100.25" is actually decimal and not integer hence it can not be converted to long.

You can use:

String strValue = "1100.25";
decimal lValue = Convert.ToDecimal(strValue);

to convert it to decimal value

BenMorel
  • 31,815
  • 47
  • 169
  • 296
sandyiit
  • 1,529
  • 3
  • 17
  • 23
1

You can also do using Int64.TryParse Method. It will return '0' if their is any string value but did not generate an error.

Int64 l1;

Int64.TryParse(strValue, out l1);
Pankaj Agarwal
  • 10,965
  • 12
  • 40
  • 58
-1

You can create your own conversion function:

    static long ToLong(string lNumber)
    {
        if (string.IsNullOrEmpty(lNumber))
            throw new Exception("Not a number!");
        char[] chars = lNumber.ToCharArray();
        long result = 0;
        bool isNegative = lNumber[0] == '-';
        if (isNegative && lNumber.Length == 1)
            throw new Exception("- Is not a number!");

        for (int i = (isNegative ? 1:0); i < lNumber.Length; i++)
        {
            if (!Char.IsDigit(chars[i]))
            {
                if (chars[i] == '.' && i < lNumber.Length - 1 && Char.IsDigit(chars[i+1]))
                {
                    var firstDigit = chars[i + 1] - 48;
                    return (isNegative ? -1L:1L) * (result + ((firstDigit < 5) ? 0L : 1L));    
                }
                throw new InvalidCastException($" {lNumber} is not a valid number!");
            }
            result = result * 10 + ((long)chars[i] - 48L);
        }
        return (isNegative ? -1L:1L) * result;
    }

It can be improved further:

  • performance wise
  • make the validation stricter in the sense that it currently doesn't care if characters after first decimal aren't digits
  • specify rounding behavior as parameter for conversion function. it currently does rounding
Cosmin Sontu
  • 913
  • 9
  • 15