C#将字符串转换为浮点值

在本文中,我们将讨论如何将字符串值转换为浮点值。 为此,我们将使用 System.BitConverter 类,它包含许多方法,其中一个是 GetBytes() 返回指定的整数值作为字节数组,另一个是 ToSingle() 方法返回一个单精度浮点数转换 字节数组中的指定位置。

C#将字符串转换为浮点值示例

using System;
using System.Collections.Generic;

using System.Linq;
using System.Text;

namespace ConsoleApplication15

{
  class Program
  {
      static void Main(string[] args)

      {
          string hexastr = "13590160";
          uint number = uint.Parse(hexastr, System.Globalization.NumberStyles.AllowHexSpecifier);

          byte[] floatvalue = BitConverter.GetBytes(number);
          float fn = BitConverter.ToSingle(floatvalue, 0);
          Console.WriteLine("float convert = {0}", fn);
          Console.ReadLine();

      }
  }
}
日期:2020-04-11 22:50:19 来源:oir作者:oir