How could I convert data from string to long in c#

前端 未结 9 2130
慢半拍i
慢半拍i 2021-02-02 04:45

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         


        
相关标签:
9条回答
  • 2021-02-02 05:14

    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
    0 讨论(0)
  • 2021-02-02 05:15

    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);
    
    0 讨论(0)
  • 2021-02-02 05:18

    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!

    0 讨论(0)
  • 2021-02-02 05:19

    This answer no longer works, and I cannot come up with anything better then the other answers (see below) listed here. Please review and up-vote them.

    Convert.ToInt64("1100.25")
    

    Method signature from MSDN:

    public static long ToInt64(
        string value
    )
    
    0 讨论(0)
  • 2021-02-02 05:19
    long l1 = Convert.ToInt64(strValue);
    

    That should do it.

    0 讨论(0)
  • 2021-02-02 05:26

    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...

    • Math.Round
    • Math.Ceil
    0 讨论(0)
提交回复
热议问题