2010-07-14 99 views
3

我有一個轉換的問題,從雙到小數點:雙轉換爲十進制

public class CartesianCoordinates 
    { 
     public int LatitudeHours { get; set;} 
     public int LatitudeMinutes { get; set; } 
     public int LatitudeSeconds { get; set; } 
     public GeoDirectionLongtitude LongitudeDirection { get; set; } 

     public int LongitudeHours { get; set; } 
     public int LongitudeMinutes { get; set; } 
     public int LongitudeSeconds { get; set; } 
     public GeoDirectionLatitude LatitudeDirection { get; set; } 
    } 

public class DecimalCoordinates 
    { 
     public decimal Latitude { get; set; } 
     public decimal Longitude { get; set; } 
    } 

CartesianCoordinates CartesianCoordinates=new CartesianCoordinates(){LatitudeHours =12,LatitudeMinutes =34,LatitudeSeconds=56 } 

    converterDecimalCoordinates.Latitude = CartesianCoordinates.LatitudeHours + (CartesianCoordinates.LatitudeMinutes + (CartesianCoordinates.LatitudeSeconds/60))/60; 

爲什麼我得到12?我想要12,55

回答

3

由於我同大衛·M和丹尼爾·布魯克納under this answerthis answer by Adam下的部分錯誤聲明自己討論的副產品,它已成爲明確表示,抱歉地說,所有的答案只是部分正確。正在發生的事情是這樣的:

// example (all x, y, z ar ints): 
Decimal d = x + y + z/60M; 

// is left to right evaluated as 
Decimal d = x + y + (((Decimal) z)/60M); 

// when doing addition, this is what happens when you add integers and something else: 
Decimal d = x + y + (int) (((Decimal) z)/60M); 

// which will yield a truncated result. 

結果是:這只是增加一個60M60.0整個語句,has been suggested,不會(或可能不會)產生想要的結果,這取決於執行順序聲明和/或加/減的存在,就像OP的問題一樣。

爲了解決這個問題,請按照亞當的建議,將每個加法/減法步驟小數,使用小數一直(不是很清楚),或放置在一個小函數,它的小數位參數的計算,迫使隱式轉換:

Decimal GetDecimalLatitude(Decimal latitudeHours, Decimal latitudeMinutes, Decimal latitudeSeconds) 
{ 
    return latitudeHours + (latitudeMinutes + (latitudeSeconds/60))/60; 
} 

作爲獎勵,它更短,並增加了可讀性。用下面的語句調用它:

converterDecimalCoordinates.Latitude = GetDecimalLatitude(
    CartesianCoordinates.LatitudeHours, 
    CartesianCoordinates.LatitudeMinutes, 
    CartesianCoordinates.LatitudeSeconds); 
+0

+1隱式鑄造//高爾夫拍手 – 2010-07-14 10:48:57

9

所有的計算都是整數,並且被四捨五入(更準確地說,被截斷)。嘗試用60m替換60的文字值以強制十進制計算,或者60.0強制進行雙重計算(在這種情況下,您需要在最後轉換爲小數)。

+4

只是一個註釋,在預期的意義上這些值不是四捨五入的 - 更多的截斷。 1.99將投1爲一個整數。 – 2010-07-14 10:01:50

3

您得到12是因爲計算在int類型上對其某些部分進行操作,因此無法包含精度 - 將值截斷爲int。在計算之前,我會將所有值轉換爲小數 - 或者如其他答案所示,將文字指定爲十進制數。

更新:在其他職位強調,這是目前執行整數除法 - 今天的東西LOL我不知道技術術語,所以喜歡你,我已經學會

+1

它只需要一位小數在計算中做小數計算。轉換所有這些都有點麻煩。 – Abel 2010-07-14 10:07:02

+0

說實話,我沒有太在意計算本身的細節 - 但我確實明白由於整數除法引入的精度損失 - 其他答案自澄清了這一點。此外,像這樣的計算通常會增加可讀性,代價是麻煩一點,這是一個很好的權衡IMO。我沒有愛:-) – 2010-07-14 10:10:26

+0

我錯了,部分地,你確實得到了你應得的愛(和積分+1),我擴大了評論線程的答案;-) – Abel 2010-07-14 10:47:13

7
Int32 x = 10; 

Decimal y = x/4; // Performs an integer devision - result is 2.0 
Decimal z = x/4M; // Performs a decimal devision - result is 2.25 

如果兩個操作數都是整數,則會得到整數分解。通過將後綴M添加到數字中,您可以明確指出該數字應該被解釋爲十進制數,因此您將得到一個十進制數。

+1

我經常碰到'Decimal x = 1M * [.. your calculation ..]'這是一種快速簡單的方法,通過將轉換字面置於前面來提高可讀性和健壯性,防止無意編輯。 – Abel 2010-07-14 10:10:55

+0

@Abel - 沒有幫助,在這種情況下,計算仍將以整數除法執行。通過乘法後轉換爲小數不會影響結果。 – 2010-07-14 10:12:02

+0

@大衛M:它確實有效。試試這個語句,它會在'x'中產生2.5M:'Decimal x = 1M * 5/2;'*(PS:我現在明白你的意思:它取決於計算和執行順序,可能並不總是一個好主意然後)*。 – Abel 2010-07-14 10:14:28

1

正如其他人已經提到,你的部門雙方都有一個整數。所以結果也是一個整數(然後隱式轉換爲左邊的小數)。爲了解決這個問題,你的部門的一方必須是一個小數,導致十進制除法。所以,簡單地嘗試這行代碼:

converterDecimalCoordinates.Latitude = CartesianCoordinates.LatitudeHours + (CartesianCoordinates.LatitudeMinutes + (CartesianCoordinates.LatitudeSeconds/60))/(decimal)60;