2013-12-10 67 views
0

我無法在我的項目中使用樓層功能。如何使用樓層功能

什麼是問題?

int numAllSms = Math.Floor((msg4SmsPart1.Count())/69) + Math.Floor((msg4SmsPart2.Count())/69) ; 

我的字符串是:

String msg4SmsPart1 = "", msg4SmsPart2 = "" ; 

這是我的錯誤:「調用以下方法或屬性之間曖昧: 'System.Math.Floor(十進制)' 和「System.Math。樓(雙)'「

+3

你得到了什麼錯誤? –

+1

你得到了什麼錯誤信息?你沒有說過什麼是錯的。 – Satal

+0

Math.Floor返回一個十進制或雙精度值,因此您可能必須先將結果賦值給一個int變量。 – acfrancis

回答

2

您至少有兩個問題:

  • Math.Floor將返回一個doubledecimal;你試圖將它分配給一個int變量
  • 你的部門正在整數算術執行,這可能不是你想要的,因爲你正在使用Math.Floor

我懷疑你想:

int numAllSms = (int) (Math.Floor(msg4SmsPart1.Count()/69.0) + 
         Math.Floor((msg4SmsPart2.Count()/69.0)); 

注意使用的69.0代替69所以,這是一個double文字,導致浮點除法。

目前尚不清楚你是否真的想要FloorCeiling雖然 - 我會預計Ceiling在這種情況下更合適。正如PSWG的回答指出,你可以只使用整數運算所有這一切 - 如果你想Ceiling相當於,你可以使用:

int numAllSms = (msg4SmsPart1.Count() + 68)/69 
       + (msg4SmsPart1.Count() + 68)/69; 

分割之前增加68使它有效輪的任何非整數結果。

0

我猜你可以使用它,但結果總是0?整數除法將不會返回您正在查找的精度。您還需要將返回值轉換爲int以匹配您要分配給的變量。嘗試在你的除法運算符兩側鑄造操作數爲雙:

int numAllSms = (int)Math.Floor((double)(msg4SmsPart1.Count())/69) + (int)Math.Floor((double)(msg4SmsPart2.Count())/69) ; 
3

Math.Floor接受並返回無論是doubledecimal,所以你必須將結果強制轉換爲int以結果ASIGN到int變量。您可能也打算在這裏執行雙倍或十進制分解。要做到這一點,最簡單的方法是寫6969.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.

而且,由於它出現msg4SmsPart1msg4SmsPart2都是字符串,這可以簡化爲:

int numAllSms = (msg4SmsPart1.Length/69) + (msg4SmsPart2.Length/69); 

或者只是

int numAllSms = msg4SmsPart1.Length/69 + msg4SmsPart2.Length/69;