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


C# Single.TryParse方法代码示例

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


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

示例1: if

string value;
float number;

// Parse a floating-point value with a thousands separator.
value = "1,643.57";
if (Single.TryParse(value, out number))
   Console.WriteLine(number);
else
   Console.WriteLine("Unable to parse '{0}'.", value);      

// Parse a floating-point value with a currency symbol and a 
// thousands separator.
value = "$1,643.57";
if (Single.TryParse(value, out number))
   Console.WriteLine(number);
else
   Console.WriteLine("Unable to parse '{0}'.", value);   

// Parse value in exponential notation.
value = "-1.643e6";
if (Single.TryParse(value, out number))
   Console.WriteLine(number);
else
   Console.WriteLine("Unable to parse '{0}'.", value);   

// Parse a negative integer value.
value = "-168934617882109132";
if (Single.TryParse(value, out number))
   Console.WriteLine(number);
else
   Console.WriteLine("Unable to parse '{0}'.", value);
开发者ID:.NET开发者,项目名称:System,代码行数:31,代码来源:Single.TryParse

输出:

1643.57
Unable to parse '$1,643.57'.
-164300
-1.68934617882109E+17

示例2: if

string value;
System.Globalization.NumberStyles style;
System.Globalization.CultureInfo culture;
float number;

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

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

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

value = "1 345,978";
if (Single.TryParse(value, style, culture, out number))
   Console.WriteLine("Converted '{0}' to {1}.", value, number);
else
   Console.WriteLine("Unable to convert '{0}'.", value);
开发者ID:.NET开发者,项目名称:System,代码行数:37,代码来源:Single.TryParse

输出:

Converted '£1,097.63' to 1097.63.
Converted '1345,978' to 1345.978.
Converted '1.345,978' to 1345.978.
Unable to convert '1 345,978'.


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