當前位置: 首頁>>代碼示例>>C#>>正文


C# Double.TryParse方法代碼示例

本文整理匯總了C#中System.Double.TryParse方法的典型用法代碼示例。如果您正苦於以下問題:C# Double.TryParse方法的具體用法?C# Double.TryParse怎麽用?C# Double.TryParse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Double的用法示例。


在下文中一共展示了Double.TryParse方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Main

//引入命名空間
using System;

public class Example
{
   public static void Main()
   {
      string[] values = { "1,643.57", "$1,643.57", "-1.643e6", 
                          "-168934617882109132", "123AE6", 
                          null, String.Empty, "ABCDEF" };
      double number;
      
      foreach (var value in values) {
         if (Double.TryParse(value, out number)) 
            Console.WriteLine("'{0}' --> {1}", value, number);
         else
            Console.WriteLine("Unable to parse '{0}'.", value);      
      }   
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:20,代碼來源:Double.TryParse

輸出:

'1,643.57' --> 1643.57
Unable to parse '$1,643.57'.
'-1.643e6' --> -1643000
'-168934617882109132' --> -1.68934617882109E+17
Unable to parse '123AE6'.
Unable to parse ''.
Unable to parse ''.
Unable to parse 'ABCDEF'.

示例2: Main

//引入命名空間
using System;

public class Example
{
   public static void Main()
   {
      string value;
      double number;
      
      value = Double.MinValue.ToString();
      if (Double.TryParse(value, out number))
         Console.WriteLine(number);
      else
         Console.WriteLine("{0} is outside the range of a Double.", 
                           value);
      
      value = Double.MaxValue.ToString();
      if (Double.TryParse(value, out number))
         Console.WriteLine(number);
      else
         Console.WriteLine("{0} is outside the range of a Double.",
                           value);
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:25,代碼來源:Double.TryParse

輸出:

-1.79769313486232E+308 is outside the range of the Double type.
1.79769313486232E+308 is outside the range of the Double type.

示例3: if

string value;
NumberStyles style;
CultureInfo culture;
double number;

// Parse currency value using en-GB culture.
value = "£1,097.63";
style = NumberStyles.Number | NumberStyles.AllowCurrencySymbol;
culture = CultureInfo.CreateSpecificCulture("en-GB");
if (Double.TryParse(value, style, culture, out number))
   Console.WriteLine("Converted '{0}' to {1}.", value, number);
else
   Console.WriteLine("Unable to convert '{0}'.", value);
// Displays: 
//       Converted '£1,097.63' to 1097.63.

value = "1345,978";
style = NumberStyles.AllowDecimalPoint;
culture = CultureInfo.CreateSpecificCulture("fr-FR");
if (Double.TryParse(value, style, culture, out number))
   Console.WriteLine("Converted '{0}' to {1}.", value, number);
else
   Console.WriteLine("Unable to convert '{0}'.", value);
// Displays:
//       Converted '1345,978' to 1345.978.

value = "1.345,978";
style = NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands;
culture = CultureInfo.CreateSpecificCulture("es-ES");
if (Double.TryParse(value, style, culture, out number))
   Console.WriteLine("Converted '{0}' to {1}.", value, number);
else
   Console.WriteLine("Unable to convert '{0}'.", value);
// Displays: 
//       Converted '1.345,978' to 1345.978.

value = "1 345,978";
if (Double.TryParse(value, style, culture, out number))
   Console.WriteLine("Converted '{0}' to {1}.", value, number);
else
   Console.WriteLine("Unable to convert '{0}'.", value);
// Displays:
//       Unable to convert '1 345,978'.
開發者ID:.NET開發者,項目名稱:System,代碼行數:43,代碼來源:Double.TryParse


注:本文中的System.Double.TryParse方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。