1. 程式人生 > >c# 型別轉換string To float

c# 型別轉換string To float

1、string To float 簡單的例子: float x = 3.14F; string s = x.ToString(); string s = "3.14"; float x = float.Parse(s);
精度問題: float x = float.Parse(s);
在提交小數的時候會出現精度丟失問題,  如果是0.5則沒有問題 資料庫中也是0.5,但是提交其他小數像 0.2  0.6  就會出現問題,資料庫中是 0.20000000298023224   0.60000002384185791 之類的資料,後來在網上查了一些資料        loat:浮點型,含位元組數為4,32bit,數值範圍為-3.4E38~3.4E38(7個有效位)  
  double:雙精度實型,含位元組數為8,64bit數值範圍-1.7E308~1.7E308(15個有效位)   decimal:數字型,128bit,28個有效位   float   與計算機中數值表示方法有關,不是準確資料.   decimal 是準確資料,不會出現這個問題 高效的string to float: public float StrToFloat(object FloatString)     {         float result;         if (FloatString != null)         {             if (float.TryParse(FloatString.ToString(), out result))
                return result;             else             {                 return (float)0.00;             }         }         else         {             return (float)0.00;         }     }