float.Parse fails on decimals and commas

前端 未结 2 1838
轻奢々
轻奢々 2020-12-31 11:26

When I try this line:

float f = float.Parse(val, System.Globalization.NumberStyles.AllowDecimalPoint | System.Globalization.NumberStyles.AllowThousands);
         


        
2条回答
  •  梦谈多话
    2020-12-31 11:57

    Parse is culture aware. If your local culture has different requirements, then you may want to pass a culture or other format provider in. Try using CultureInfo.InvariantCulture. You won't need the decimal option if you do.

    float f = float.Parse(val,
                          System.Globalization.NumberStyles.AllowThousands,
                          CultureInfo.InvariantCulture);
    

提交回复
热议问题