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


C# Decimal.Parse方法代碼示例

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


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

示例1: CultureInfo

string value;
decimal number;
NumberStyles style;
CultureInfo provider;

// Parse string using "." as the thousands separator 
// and " " as the decimal separator. 
value = "892 694,12";
style = NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands;
provider = new CultureInfo("fr-FR");

number = Decimal.Parse(value, style, provider);  
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays: 
//    892 694,12' converted to 892694.12.

try
{
   number = Decimal.Parse(value, style, CultureInfo.InvariantCulture);  
   Console.WriteLine("'{0}' converted to {1}.", value, number);
}   
catch (FormatException)
{
   Console.WriteLine("Unable to parse '{0}'.", value);
}
// Displays: 
//    Unable to parse '892 694,12'.  

// Parse string using "$" as the currency symbol for en-GB and  
// en-us cultures.
value = "$6,032.51";
style = NumberStyles.Number | NumberStyles.AllowCurrencySymbol;
provider = new CultureInfo("en-GB");

try
{
   number = Decimal.Parse(value, style, provider);  
   Console.WriteLine("'{0}' converted to {1}.", value, number);
}   
catch (FormatException)
{
   Console.WriteLine("Unable to parse '{0}'.", value);
}
// Displays: 
//    Unable to parse '$6,032.51'.

provider = new CultureInfo("en-US");
number = Decimal.Parse(value, style, provider);  
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays: 
//    '$6,032.51' converted to 6032.51.
開發者ID:.NET開發者,項目名稱:System,代碼行數:51,代碼來源:Decimal.Parse

示例2: OkToDecimal_Click

protected void OkToDecimal_Click(object sender, EventArgs e)
{
   string locale;
   decimal number;
   CultureInfo culture;

   // Return if string is empty
   if (String.IsNullOrEmpty(this.inputNumber.Text))
      return;

   // Get locale of web request to determine possible format of number
   if (Request.UserLanguages.Length == 0)
      return;
   locale = Request.UserLanguages[0];
   if (String.IsNullOrEmpty(locale))
      return;

  // Instantiate CultureInfo object for the user's locale
   culture = new CultureInfo(locale);

   // Convert user input from a string to a number
   try
   {
      number = Decimal.Parse(this.inputNumber.Text, culture.NumberFormat);
   }
   catch (FormatException)
   {
      return;
   }
   catch (Exception)
   {
      return;
   }
   // Output number to label on web form
   this.outputNumber.Text = "Number is " + number.ToString();
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:36,代碼來源:Decimal.Parse

示例3: catch

string value;
decimal number;
// Parse an integer with thousands separators. 
value = "16,523,421";
number = Decimal.Parse(value);
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays: 
//    16,523,421' converted to 16523421.

// Parse a floating point value with thousands separators
value = "25,162.1378";
number = Decimal.Parse(value);
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays: 
//    25,162.1378' converted to 25162.1378.

// Parse a floating point number with US currency symbol.
value = "$16,321,421.75";
try
{
   number = Decimal.Parse(value);
   Console.WriteLine("'{0}' converted to {1}.", value, number);
}   
catch (FormatException)
{
   Console.WriteLine("Unable to parse '{0}'.", value);
}
// Displays:
//    Unable to parse '$16,321,421.75'.   

// Parse a number in exponential notation
value = "1.62345e-02";
try
{
   number = Decimal.Parse(value);
   Console.WriteLine("'{0}' converted to {1}.", value, number);
}   
catch (FormatException)
{
   Console.WriteLine("Unable to parse '{0}'.", value);
}
// Displays: 
//    Unable to parse '1.62345e-02'.
開發者ID:.NET開發者,項目名稱:System,代碼行數:43,代碼來源:Decimal.Parse

示例4: catch

string value;
decimal number;
NumberStyles style;

// Parse string with a floating point value using NumberStyles.None. 
value = "8694.12";
style = NumberStyles.None;
try
{
   number = Decimal.Parse(value, style);  
   Console.WriteLine("'{0}' converted to {1}.", value, number);
}
catch (FormatException)
{
   Console.WriteLine("Unable to parse '{0}'.", value);
}
// Displays:
//    Unable to parse '8694.12'.

// Parse string with a floating point value and allow decimal point. 
style = NumberStyles.AllowDecimalPoint;
number = Decimal.Parse(value, style);  
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays:
//    '8694.12' converted to 8694.12.

// Parse string with negative value in parentheses
value = "(1,789.34)";
style = NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands | 
        NumberStyles.AllowParentheses; 
number = Decimal.Parse(value, style);  
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays:
//    '(1,789.34)' converted to -1789.34.

// Parse string using Number style
value = " -17,623.49 ";
style = NumberStyles.Number;
number = Decimal.Parse(value, style);  
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays:
//    ' -17,623.49 ' converted to -17623.49.
開發者ID:.NET開發者,項目名稱:System,代碼行數:42,代碼來源:Decimal.Parse


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