Convert.ToDouble(“4089.90”) outputs 40.899,00 why?

前端 未结 6 2124
傲寒
傲寒 2021-02-20 16:55

I am developing a software that uses number precision, but I have this problem, it happens that when I take a string to convert to double it outputs me with a different culture.

相关标签:
6条回答
  • 2021-02-20 17:34

    It is your computer that is not giving the correct answer, not theirs. Your culture states that "4089.90" is the same as 4089900, since the dot (.) is used for separating thousands (and thus there should be three numbers after the dot).

    It appears you want to use the dot as a decimal-separator, contrary to your culture settings; so you have to use System.Globalization.CultureInfo.InvariantCulture in your program. Sorry.

    0 讨论(0)
  • 2021-02-20 17:40

    I know neither c# nor asp.net, but I think the problem is this: You are performing the operation in a culture where the dot . is the thousands separator and not the decimal separator. The very output you quote is the proof: 40.899,00.

    What culture/locale are you working in?

    0 讨论(0)
  • 2021-02-20 17:46

    You can set the culture for your thread with:

    Thread.CurrentThread.CurrentCulture = 
           System.Globalization.CultureInfo.InvariantCulture;
    
    0 讨论(0)
  • 2021-02-20 17:48

    Actually is very rare this behaviour cause all the machines have the same culture settings, however I think that the best solution would be to modify the web.config like this:

    configuration>
      <system.web>
        <globalization culture = "es-HN" />
      </system.web>
    </configuration
    

    And apply the settings for the entire application.

    Thanx everyone for your assistance.

    0 讨论(0)
  • 2021-02-20 17:53

    Culture could be based on where the ASP.Net application is running, and not the client PC that is running the browser. While their desktop PC might have similar culture settings, the server may differ.

    0 讨论(0)
  • 2021-02-20 17:54

    You don't say where you are based, but the output is consistent with the current culture being one that has "." as the thousands separator and a decimal comma rather than a decimal point.

    However, you state that the culture is the same - which contradicts this. Have you or the client changed (or customised) the "Standards and formats" on the Regional and Language Options? If the setting has been customised it will still read as "English (United Kingdom)" (or where ever) but will produce different results to the default.

    0 讨论(0)
提交回复
热议问题