c# 類型轉換string To float

分類:編程 時間:2016-11-06
[摘要: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); 正在提]

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;         }     }
Tags:

文章來源:


ads
ads

相關文章
ads

相關文章

ad