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

人走茶凉 提交于 2019-12-03 08:52:13

问题


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;

回答1:


Convert.ToInt64("1100.25")

Method signature from MSDN:

public static long ToInt64(
    string value
)



回答2:


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



回答3:


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.




回答4:


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

long l1;
l1 = long.Parse("1100.25");
//or
long.TryParse("1100.25", out l1);



回答5:


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!




回答6:


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




回答7:


long l1 = Convert.ToInt64(strValue);

That should do it.




回答8:


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);



回答9:


long=convert.toDouble("strvalue")


来源:https://stackoverflow.com/questions/6330306/how-could-i-convert-data-from-string-to-long-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!