回答
如果你想要得到這個數字的整數部分,你必須先將其轉換爲浮動號碼,然後轉換爲長。
long l1 = (long)Convert.ToDouble("1100.25");
您可以使用Math
類圍捕號碼,只要你喜歡,或者只是截斷......
如果你在指定的輸入上運行這個命令,會給你'FormatException'。 – 2011-06-13 12:22:18
@zespri,不,至少在大多數文化中不是這樣......但在小數分隔符不是「」的文化中確實會失敗。 – 2011-06-13 12:23:53
感謝您的編輯。這個更好。 – 2011-06-13 12:28:21
http://msdn.microsoft.com/en-us/library/system.convert.aspx
l1 = Convert.ToInt64(strValue)
雖然你給的例子不是一個整數,所以我不知道你爲什麼想要它作爲一項長期的。
長L1 = Convert.ToInt64(strValue中);
應該這樣做。
您還可以使用Int64.TryParse
方法。如果它們是任何字符串值,但它不會產生錯誤,它將返回'0'。
Int64 l1;
Int64.TryParse(strValue, out l1);
long在內部表示爲System.Int64,它是一個64位有符號整數。 您採取的值「1100.25」實際上是十進制數,而不是整數,因此它不能轉換爲長整型。
您可以使用:
String strValue = "1100.25";
decimal lValue = Convert.ToDecimal(strValue);
將其轉換爲十進制值
您將無法將其直接轉換爲長,因爲小數點,我認爲你應該把它轉換成十進制然後將其轉換成長這樣的東西:
String strValue[i] = "1100.25";
long l1 = Convert.ToInt64(Convert.ToDecimal(strValue));
希望這有助於!從MSDN
Convert.ToInt64("1100.25")
方法簽名:
public static long ToInt64(
string value
)
這是我做到的。應該標記爲正確的答案。 – sanpaco 2013-10-17 18:55:07
它會給字符串格式不正確的異常。 – 2016-11-18 10:57:34
- 1. 我怎麼能存儲的int數組轉換爲字符串
- 2. 我怎麼會一個字符串數據轉換爲圖像
- 3. 爲什麼我不能將整數轉換爲字符串?
- 4. 爲什麼我不能將字符串轉換爲整數
- 5. 我怎麼能從字符串轉換jsonarray
- 6. 我怎麼能轉換爲字符串翻番沒有迅速
- 7. 不能將字符串隱式轉換爲字符串[]。在c#
- 8. C#將字符串轉換爲數字
- 9. PHP:我怎樣才能將索引轉換爲字符串?
- 10. 我怎麼行值從SQL Server轉換爲字符串在C#中
- 11. 我怎麼添加字符串擴展將字符串轉換爲UTF8
- 12. 將字符串轉換爲整數C#
- 13. 將數組轉換爲字符串? c#
- 14. C++ - 將字符串轉換爲字符
- 15. 將字符串轉換爲數組(不是你怎麼看)
- 16. 將字符串轉換爲長
- 17. 將字符串轉換爲長
- 18. 將長字符串轉換爲日期
- 19. 將長字符串轉換爲data.frame
- 20. 將字符串變量轉換爲長
- 21. Asp.Net:將字符串轉換爲長
- 22. 無法將字符串轉換爲長
- 23. 從字符串轉換爲字符 - C++
- 24. 我怎麼能轉換長日期,年?
- 25. C#錯誤:不能將字符串[]'轉換爲'字符串'
- 26. 爲什麼我們不能將字符串轉換爲stringbuffer?
- 27. 將字符串轉換爲數據集
- 28. 將JSON數據轉換爲字符串
- 29. 在C++中將字符數組轉換爲整數字符串
- 30. 將字符串轉換爲int在C++
多頭代表整數。你已經給出了一個非整數。你想怎麼做? – 2011-06-13 12:21:14
如果strValue是「1100.75」會怎麼樣?請參閱:http://stackoverflow.com/questions/633335/how-might-i-convert-a-double-to-the-nearest-integer-value – magma 2011-06-13 12:22:28
@magma:如果你將它轉換成十進制然後將圓數字,即在這種情況下,如果給定的數字是1100.75,那麼它將輸出1101. – 2011-06-13 12:47:02