Converting String To Float in C#

后端 未结 7 727
渐次进展
渐次进展 2020-11-28 22:12

I am converting a string like \"41.00027357629127\", and I am using;

Convert.ToSingle(\"41.00027357629127\");

or

float.Pars         


        
相关标签:
7条回答
  • 2020-11-28 22:25

    First, it is just a presentation of the float number you see in the debugger. The real value is approximately exact (as much as it's possible).

    Note: Use always CultureInfo information when dealing with floating point numbers versus strings.

    float.Parse("41.00027357629127",
          System.Globalization.CultureInfo.InvariantCulture);
    

    This is just an example; choose an appropriate culture for your case.

    0 讨论(0)
  • 2020-11-28 22:25

    You can double.Parse("41.00027357629127");

    0 讨论(0)
  • 2020-11-28 22:32

    Use Convert.ToDouble("41.00027357629127");

    Convert.ToDouble documentation

    0 讨论(0)
  • 2020-11-28 22:35

    You can use parsing with double instead of float to get more precision value.

    0 讨论(0)
  • 2020-11-28 22:50

    Your thread's locale is set to one in which the decimal mark is "," instead of ".".

    Try using this:

    float.Parse("41.00027357629127", CultureInfo.InvariantCulture.NumberFormat);
    

    Note, however, that a float cannot hold that many digits of precision. You would have to use double or Decimal to do so.

    0 讨论(0)
  • 2020-11-28 22:50

    You can use the following:

    float asd = (float) Convert.ToDouble("41.00027357629127");
    
    0 讨论(0)
提交回复
热议问题