本文整理汇总了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"));
}
}
示例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.");
}
输出:
-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();
}
示例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;
}
}
}
示例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();
}
输出:
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);
}
}