当前位置: 首页>>代码示例>>C#>>正文


C# Double.Parse方法代码示例

本文整理汇总了C#中System.Double.Parse方法的典型用法代码示例。如果您正苦于以下问题:C# Double.Parse方法的具体用法?C# Double.Parse怎么用?C# Double.Parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Double的用法示例。


在下文中一共展示了Double.Parse方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Parse

//引入命名空间
using System;
using System.Globalization;

public class Temperature
{
   // Parses the temperature from a string. Temperature scale is 
   // indicated by 'F (for Fahrenheit) or 'C (for Celcius) at the end
   // of the string.
   public static Temperature Parse(string s, NumberStyles styles, 
                                   IFormatProvider provider)
   {                                    
      Temperature temp = new Temperature();

      if (s.TrimEnd(null).EndsWith("'F"))
      {
         temp.Value = Double.Parse(s.Remove(s.LastIndexOf((char)39), 2), 
                                   styles, provider);
      }
      else
      {
         if (s.TrimEnd(null).EndsWith("'C"))
            temp.Celsius = Double.Parse(s.Remove(s.LastIndexOf((char)39), 2), 
                                        styles, provider);
         else
            temp.Value = Double.Parse(s, styles, provider);         
      }
      return temp;      
   } 
   
   // Declare private constructor so Temperature so only Parse method can
   // create a new instance
   private Temperature()   {}

   protected double m_value;
   
   public double Value 
   {
      get { return m_value; }
      private set { m_value = value; }
   }
   
   public double Celsius
   {
      get { return (m_value - 32) / 1.8; }
      private set { m_value = value * 1.8 + 32; }
   }
   
   public double Fahrenheit
   {
      get {return m_value; }
   }      
}

public class TestTemperature
{
   public static void Main()
   {
      string value;
      NumberStyles styles;
      IFormatProvider provider;
      Temperature temp;
      
      value = "25,3'C";
      styles = NumberStyles.Float;
      provider = CultureInfo.CreateSpecificCulture("fr-FR");
      temp = Temperature.Parse(value, styles, provider);
      Console.WriteLine("{0} degrees Fahrenheit equals {1} degrees Celsius.", 
                        temp.Fahrenheit, temp.Celsius);
      
      value = " (40) 'C";
      styles = NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite 
               | NumberStyles.AllowParentheses;
      provider = NumberFormatInfo.InvariantInfo;
      temp = Temperature.Parse(value, styles, provider);
      Console.WriteLine("{0} degrees Fahrenheit equals {1} degrees Celsius.", 
                        temp.Fahrenheit, temp.Celsius);
      
      value = "5,778E03'C";      // Approximate surface temperature of the Sun
      styles = NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands |
               NumberStyles.AllowExponent;
      provider = CultureInfo.CreateSpecificCulture("en-GB"); 
      temp = Temperature.Parse(value, styles, provider);
      Console.WriteLine("{0} degrees Fahrenheit equals {1} degrees Celsius.", 
                        temp.Fahrenheit.ToString("N"), temp.Celsius.ToString("N"));
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:87,代码来源:Double.Parse

示例2: catch

string value;
   
   value = Double.MinValue.ToString();
   try {
      Console.WriteLine(Double.Parse(value));
   }   
   catch (OverflowException) {
      Console.WriteLine($"{value} is outside the range of the Double type.");
   }
   
   value = Double.MaxValue.ToString();
   try {
      Console.WriteLine(Double.Parse(value));
   }
   catch (OverflowException) {
      Console.WriteLine($"{value} is outside the range of the Double type.");
   }

   // Format without the default precision.
   value = Double.MinValue.ToString("G17");
   try 
   {
       Console.WriteLine(Double.Parse(value));
   }
   catch (OverflowException)
   {
      Console.WriteLine($"{value} is outside the range of the Double type.");
   }
开发者ID:.NET开发者,项目名称:System,代码行数:28,代码来源:Double.Parse

输出:

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

示例3: OkToDouble_Click

protected void OkToDouble_Click(object sender, EventArgs e)
{
   string locale;
   double 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 = Double.Parse(this.inputNumber.Text, culture.NumberFormat);
   }
   catch (FormatException)
   {
      return;
   }
   catch (OverflowException)
   {
      return;
   }
   // Output number to label on web form
   this.outputNumber.Text = "Number is " + number.ToString();
}
开发者ID:.NET开发者,项目名称:System,代码行数:36,代码来源:Double.Parse

示例4: Parse

public class Temperature {
    // Parses the temperature from a string in form
    // [ws][sign]digits['F|'C][ws]
    public static Temperature Parse(string s) {
        Temperature temp = new Temperature();

        if( s.TrimEnd(null).EndsWith("'F") ) {
            temp.Value = Double.Parse( s.Remove(s.LastIndexOf('\''), 2) );
        }
        else if( s.TrimEnd(null).EndsWith("'C") ) {
            temp.Celsius = Double.Parse( s.Remove(s.LastIndexOf('\''), 2) );
        }
        else {
            temp.Value = Double.Parse(s);
        }

        return temp;
    }

    // The value holder
    protected double m_value;

    public double Value {
        get {
            return m_value;
        }
        set {
            m_value = value;
        }
    }

    public double Celsius {
        get {
            return (m_value-32.0)/1.8;
        }
        set {
            m_value = 1.8*value+32.0;
        }
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:40,代码来源:Double.Parse

示例5: Main

public static void Main()
{
   // Set current thread culture to en-US.
   Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
   
   string value;
   NumberStyles styles;
   
   // Parse a string in exponential notation with only the AllowExponent flag. 
   value = "-1.063E-02";
   styles = NumberStyles.AllowExponent;
   ShowNumericValue(value, styles);
   
   // Parse a string in exponential notation
   // with the AllowExponent and Number flags.
   styles = NumberStyles.AllowExponent | NumberStyles.Number;
   ShowNumericValue(value, styles);

   // Parse a currency value with leading and trailing white space, and
   // white space after the U.S. currency symbol.
   value = " $ 6,164.3299  ";
   styles = NumberStyles.Number | NumberStyles.AllowCurrencySymbol;
   ShowNumericValue(value, styles);
   
   // Parse negative value with thousands separator and decimal.
   value = "(4,320.64)";
   styles = NumberStyles.AllowParentheses | NumberStyles.AllowTrailingSign |
            NumberStyles.Float; 
   ShowNumericValue(value, styles);
   
   styles = NumberStyles.AllowParentheses | NumberStyles.AllowTrailingSign |
            NumberStyles.Float | NumberStyles.AllowThousands;
   ShowNumericValue(value, styles);
}

private static void ShowNumericValue(string value, NumberStyles styles)
{
   double number;
   try
   {
      number = Double.Parse(value, styles);
      Console.WriteLine("Converted '{0}' using {1} to {2}.", 
                        value, styles.ToString(), number);
   }
   catch (FormatException)
   {
      Console.WriteLine("Unable to parse '{0}' with styles {1}.", 
                        value, styles.ToString());
   }
   Console.WriteLine();                           
}
开发者ID:.NET开发者,项目名称:System,代码行数:51,代码来源:Double.Parse

输出:

Unable to parse '-1.063E-02' with styles AllowExponent.

Converted '-1.063E-02' using AllowTrailingSign, AllowThousands, Float to -0.01063.

Converted ' $ 6,164.3299  ' using Number, AllowCurrencySymbol to 6164.3299.

Unable to parse '(4,320.64)' with styles AllowTrailingSign, AllowParentheses, Float.

Converted '(4,320.64)' using AllowTrailingSign, AllowParentheses, AllowThousands, Float to -4320.64.

示例6: Main

//引入命名空间
using System;

public class NumericParsing
{
    public static void Main()
    {
        int value = Int32.Parse("99953");
        double dval = Double.Parse("1.3433E+35");
        Console.WriteLine("{0}", value);
        Console.WriteLine("{0}", dval);
    }
}
开发者ID:C#程序员,项目名称:System,代码行数:13,代码来源:Double.Parse


注:本文中的System.Double.Parse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。