Convert To Double

前端 未结 3 1184
太阳男子
太阳男子 2021-01-29 05:54

Ok im sitting with this issue kind of stupid but I don\'t know how to google this for the solution.

Lets say i have a database with values inside e.g 1000.00

相关标签:
3条回答
  • 2021-01-29 06:33

    See Convert.ToDouble(string,CultureInfo)

    0 讨论(0)
  • 2021-01-29 06:48

    The locale setting, specifically the regional settings that govern what symbol is used as a decimal point, are influencing the conversion. To avoid that, you need to perform a culture invariant conversion so that the locale setting on your machine does not play a part. For example:

    double value = double.Parse(stringValue, CultureInfo.InvariantCulture);
    

    That said, I do wonder why you are storing floating point values as strings in the first place. It's best to convert user input into its natural form at the earliest possible moment, and only convert back to string as late as possible.

    0 讨论(0)
  • 2021-01-29 06:50

    This is because of your CultureInfo. Apparently, your PC is using periods for decimal notation, while your server is using commas. if you try to convert one of them using the other method, you get this issue.

    Solution: Convert.ToDouble() has an extra overload which allows you to pass a cultureInfo for conversion. Or you can just save your variable in the database as a float or decimal.

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