2013-10-19 67 views
1

您好我想轉換stringfloat 我嘗試用float.Parse() 問題字符串格式是一些用戶的##,###,並與其他##。 ###這取決於用戶CultureInfo 當我字符串轉換爲浮動我有一個錯誤,因爲浮點格式是「」 所以如何管理?轉換字符串的CultureInfo

+1

什麼是您所使用的確切的代碼,以及樣品浮... 這可能幫助:http://stackoverflow.com/questions/6171202/c-sharp-parsing-float-from-string –

回答

0

只要按照從Double.TryParse Method的例子。不要指定文化,只要向系統詢問當前的文化。

string userInput = "56,67"; 
    float output; 
    System.Globalization.NumberStyles style = System.Globalization.NumberStyles.AllowDecimalPoint; 
    System.Globalization.CultureInfo info = System.Threading.Thread.CurrentThread.CurrentCulture; 

    if (float.TryParse(userInput, style, info, out output)) 
    { 
     // ... 
    } 
    else 
    { 
     // .... 
    } 
0

使用它看起來低於過載

float.Parse("20,30", System.Globalization.NumberStyles.Float, 
          Thread.CurrentThread.CurrentUICulture); 

或者你也可以使用double.parse如下

double.Parse("30.30", System.Globalization.NumberStyles.Float, 
          Thread.CurrentThread.CurrentUICulture);