Math.Floor
接受並返回無論是double
或decimal
,所以你必須將結果強制轉換爲int
以結果ASIGN到int
變量。您可能也打算在這裏執行雙倍或十進制分解。要做到這一點,最簡單的方法是寫69
爲69.0
(雙)或69m
(十進制):
int numAllSms = (int)(Math.Floor((msg4SmsPart1.Count())/69m) + Math.Floor((msg4SmsPart2.Count())/69m));
但是,因爲你處理整數已經,您可以直接跳過調用Math.Floor
並利用整數算術:
int numAllSms = (msg4SmsPart1.Count()/69) + (msg4SmsPart2.Count()/69);
見/ Operator (C# Reference):
When you divide two integers, the result is always an integer. For example, the result of 7/3 is 2.
而且,由於它出現msg4SmsPart1
和msg4SmsPart2
都是字符串,這可以簡化爲:
int numAllSms = (msg4SmsPart1.Length/69) + (msg4SmsPart2.Length/69);
或者只是
int numAllSms = msg4SmsPart1.Length/69 + msg4SmsPart2.Length/69;
你得到了什麼錯誤? –
你得到了什麼錯誤信息?你沒有說過什麼是錯的。 – Satal
Math.Floor返回一個十進制或雙精度值,因此您可能必須先將結果賦值給一個int變量。 – acfrancis