Exception while parsing negative double numbers in C#

后端 未结 5 1637
忘了有多久
忘了有多久 2021-01-07 23:53

I\'m coding a peace of code that extracts some data from a DB. And the problem is that I want to convert a negative number string \"−2.8\" to a double. Pretty easy, I though

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-08 00:29

    You have a character that is not the minus character used in numbers (you have hyphen, not dash). You have to replace it, there's no other "elegant" way. Those two characters only visually resemble each other but they are not meant to replace each other without a change of meaning.

    var climateString = "−2.8";
    var number = double.Parse(climateString.Replace("−","-"));
    

提交回复
热议问题