2016-12-10 67 views
1

如何解析挪威貨幣到小數?如何解析挪威貨幣

我試圖解析這些:

477,60 
2.320,00 

此代碼不能正常工作,並拋出,即使我已指定挪威的文化解析小數解析異常。

Convert.ToDecimal("2.320,00", System.Globalization.CultureInfo.GetCultureInfo("nb-NO")) 
+0

挪威或者是 「NB-NO」 或 「NN-NO」,不這個工作如果你使用「nn-NO」? –

+0

@PeteStensønes不,它不會與其他文化一起工作,它們都沒有定義NumberGroupSeparator。 – mybirthname

+0

只是爲了確保:這實際上是格式化貨幣的挪威方式嗎? glibc中的千位分隔符是一個不間斷的空間,這是由許多網站(一個例子http://mylittlenorway.com/2009/11/crosses-commas-and-great-divides/)備份,但不是我自己是挪威人,我無法確定。這很重要,因爲它影響你應該如何解決問題。如果這實際上是一種自定義格式,那麼解析時的自定義代碼就很有意義。如果這是挪威某些地方的標準,那麼修改文化信息並在整個應用程序中使用它是有道理的。 – hvd

回答

3

因此,挪威文化沒有定義NumberFormat.NumberGroupSeparator,因爲您收到此異常。所以,你需要定義他們:

CultureInfo info = CultureInfo.CreateSpecificCulture("nb-NO"); 
var numberFormat = info.NumberFormat; 
numberFormat.NumberGroupSeparator = "."; 
numberFormat.CurrencyGroupSeparator = ".";//this if you are using currency 
numberFormat.PercentGroupSeparator = ".";//this for percentages 

後嘗試使用decimal.TryParse方法:

decimal result = 0; 
decimal.TryParse("2.320,00", NumberStyles.AllowDecimalPoint|NumberStyles.AllowThousands, info, out result); 

這裏Full Example

-1

嘗試: ''

Convert.ToDecimal("2320,00", System.Globalization.CultureInfo.GetCultureInfo("nb-NO")); 

,因爲我已經檢查它不喜歡任何''''之後'

+0

對不起,你這是什麼意思?我們無法自定義挪威貨幣的格式。 –