2012-02-25 57 views
1

我試圖從2 Decimal構造一個GeoPoint。我面臨的問題是GeoPoint構造函數需要E6格式的2個整數。因此,與文字很容易:如何將十進制數轉換爲int E6

GeoPoint point = new GeoPoint((int)61.54367E6, (int)-149.768E6); 

但如果我的輸入被存儲爲Decimal我該怎麼辦?

Decimal Latitude = 61.54367; 
Decimal Longitude = -149.768; 

回答

4
GeoPoint point = new GeoPoint((int)(Latitude * 1E6), (int)(Longitude * 1E6)); 
+0

完美。非常感謝。 – 2012-02-25 00:21:50

+0

歡迎您:) – 2012-02-25 00:24:52

1

你在談論的 「E6」 的格式是科學記數法。這就是浮點文字的寫法。請參見§ 9.4.4.2(整數字符)和§ 9.4.4.3 C#標準的實數字(文字):ISO/IEC 23270(信息技術—編程語言— C#)。 ISO/IEC 23270和它的兄弟,ISO/IEC 23271,描述了CLR,是免費提供從ISO PDF文件在

http://standards.iso.org/ittf/PubliclyAvailableStandards/index.html

但所有你需要做的就是投你decimaldouble

new GeoPoint((double) latitude , (double) longitude) ; 

,或者,使用靜態Decimal.ToDouble()方法:

new GeoPoint(Decimal.ToDouble(latitude) , Decimal.ToDouble(longitude)) ; 

不作任何DIF你做什麼。同樣的事情發生在封面之下。

但是,您應該注意,您可能會失去雙倍轉換精度。 decimal交易範圍(大小)爲精度; doublefloat範圍(幅度)的交易精度。另外,如果要轉換的值不能表示爲decimal,則從doubledecimal的轉換將拋出OverflowException

下面的任何值的將引發OverflowException

  • Double.NaN
  • Double.PositiveInfinity
  • Double.NegativeInfinity
  • 值小於Decimal.MinValue
  • 值比Decimal.MaxValue
更大
+0

謝謝您提供的其他信息Nicholas – 2012-02-25 01:21:52

相關問題