How to convert “12,4” to decimal en-Us culture

前端 未结 7 2171
梦如初夏
梦如初夏 2020-12-16 02:24

I have a decimal value (\"133,3\") stored in string column in the database, in norway culture.

after that user changed the regional setting to english-Us. when I con

7条回答
  •  粉色の甜心
    2020-12-16 03:03

    used below code to fix my issue. I just hard coded the previous currency decimal part. may not be generic. but solved my problem.

    public static decimal? ToDecimal1(this string source)
        {
            CultureInfo usCulture = new CultureInfo("en-US");
    
            if (string.IsNullOrEmpty(source.Trim1()))
                return null;
            else
                return Convert.ToDecimal(source.Replace(",", ".").Trim(), usCulture);
        }
    

提交回复
热议问题