Double.Parse - Internationalization problem

前端 未结 6 664
故里飘歌
故里飘歌 2020-12-17 10:04

This is driving me crazy. I have the following string in a ASP.NET 2.0 WebForm Page

string s = \"0.009\";

Simple enough. Now, if my culture

6条回答
  •  我在风中等你
    2020-12-17 11:01

    You have it backwards.

    When you say double d = Double.Parse(s, new CultureInfo("es-ES"));, you are asking .NET to parse your string into a double, assuming that the string is written in the es-ES culture.

    In Spanish culture, "." is a thousands separator, so "0.009" is 9.

    When you convert using ToString(), at the end, it's saying convert 0.009 to a string using the spanish culture, so it uses "," as the decimal separator, and you get "0,009". The behavior is correct.

    My guess is that you want to use Double.Parse with the Invariant Culture, and ToString with the spanish culture, so 0.009 becomes 0,009.

提交回复
热议问题