2015年4月8日 星期三

先前曾部署程式到對岸,沒有明顯的文化差異,也就沒遇到Format、時差等問題
這回對象改成南非(較台灣慢6小時),程式出現「Input string was not in a correct format」錯誤
在台灣機器可正常執行,還真不曉得問題是停在哪一段程式碼
有經驗的同事提到應該跟Format有關,好吧,打開控制台修改環境

第1步新增語言English(South Africa),記得將它置頂
image
第2步將格式(Format)改成英文(南非)
SNAGHTML9ea910
接著進行偵錯,果然出現和南非機器相同錯誤訊息
image
找到了,問題就在decimal.Parse,在南非居然行不通
註:number=” 0.00”
image

Decimal.Parse 有提供IFormatProvider的多載方法,因此我們將Format設定為CultureInfo.InvariantCulture(不因文化特性而異是不區分文化特性的;它與英文有關但並非與所有國家/地區有關)
image
重新建置後,台灣和南非機器都能正常執行,真是太棒了…^^

最後還是要認識一下decimal在南非的用法,1個空格代表千分號,逗號代表小數點
South Africa officially uses a decimal comma, with a space as thousands separators.
Example:
1 450 789,32 = one million; four hundred thousand; fifty thousand; zero thousands;  seven hundreds; eighty; nine; three tenths; two hundredths.

參考:
1、Decimal.Parse 方法 (String, IFormatProvider)
2、CultureInfo.InvariantCulture 屬性

Post a Comment: