How do I parse a string with a decimal point to a double?

前端 未结 19 1550
孤街浪徒
孤街浪徒 2020-11-22 06:47

I want to parse a string like \"3.5\" to a double. However,

double.Parse(\"3.5\") 

yields 35 and

double.Pars         


        
19条回答
  •  Happy的楠姐
    2020-11-22 07:02

    Instead of having to specify a locale in all parses, I prefer to set an application wide locale, although if string formats are not consistent across the app, this might not work.

    CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("pt-PT");
    CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("pt-PT");
    

    Defining this at the begining of your application will make all double parses expect a comma as the decimal delimiter. You can set an appropriate locale so that the decimal and thousands separator fits the strings you are parsing.

提交回复
热议问题