技術(shù)頻道導(dǎo)航
HTML/CSS
.NET技術(shù)
IIS技術(shù)
PHP技術(shù)
Js/JQuery
Photoshop
Fireworks
服務(wù)器技術(shù)
操作系統(tǒng)
網(wǎng)站運(yùn)營(yíng)

贊助商

分類目錄

贊助商

最新文章

搜索

詳解C# TryParse(String, NumberStyles, IFormatProvider, Single)轉(zhuǎn)換字符串為浮點(diǎn)數(shù)

作者:admin    時(shí)間:2022-5-27 11:2:37    瀏覽:

前面文章介紹了single.tryParse將數(shù)字字符串轉(zhuǎn)換為浮點(diǎn)數(shù)的方法,但是如果要顯式定義可以出現(xiàn)在字符串中的元素(例如貨幣符號(hào)、千位分隔符和空格),需要使用TryParse(String, NumberStyles, IFormatProvider, Single)方法重載,這是本文要介紹的內(nèi)容。

TryParse(String, NumberStyles, IFormatProvider, Single)

描述

將指定樣式和特定區(qū)域性格式的數(shù)字的字符串表示形式轉(zhuǎn)換為其等效的單精度浮點(diǎn)數(shù)。返回值指示轉(zhuǎn)換是成功還是失敗。

語(yǔ)法

public static bool TryParse (string? s, System.Globalization.NumberStyles style, IFormatProvider? provider, out float result);

參數(shù)

  • s String:包含要轉(zhuǎn)換的數(shù)字的字符串。
  • style NumberStyles:枚舉值的按位組合,典型值是FloatAllowThousands的組合。
  • provider IFormatProvider:提供有關(guān)s的區(qū)域性特定格式信息的對(duì)象。
  • result Single:當(dāng)此方法返回時(shí),如果轉(zhuǎn)換成功,則包含與s中包含的數(shù)值或符號(hào)等效的單精度浮點(diǎn)數(shù),如果轉(zhuǎn)換失敗,則返回零。如果s參數(shù)是nullEmpty、不符合style的格式,或者style不是NumberStyles枚舉常量的有效組合,則轉(zhuǎn)換失敗。如果s表示小于MinValue或大于MaxValue的數(shù)字,它在 .NET Framework 或 .NET Core 2.2 及更早版本上也會(huì)失敗。此參數(shù)未初始化傳遞;最初提供的任何值result都將被覆蓋。

返回

布爾值:true,如果s轉(zhuǎn)換成功;否則,false。

例外

style不是NumberStyles值。

-或者-

style不是AllowHexSpecifierHexNumber值的組合。

使用示例

下面的示例演示了使用Single.TryParse(String, NumberStyles, IFormatProvider, Single)方法來(lái)解析具有特定樣式并使用特定區(qū)域性的約定格式化的數(shù)字的字符串表示形式。

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);
// The example displays the following output:
// 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'.

評(píng)論

在 .NET Core 3.0 及更高版本中,太大而無(wú)法表示的值會(huì)按照 IEEE 754 規(guī)范的要求四舍五入為PositiveInfinityNegativeInfinity 。在包括 .NET Framework 在內(nèi)的早期版本中,解析太大而無(wú)法表示的值會(huì)導(dǎo)致失敗。

此重載與Parse(String, NumberStyles, IFormatProvider)方法的不同之處在于返回一個(gè)布爾值,該值指示解析操作是否成功,而不是返回解析后的數(shù)值。它消除了在s無(wú)效且無(wú)法成功解析的事件中使用異常處理來(lái)測(cè)試FormatException的需要。

style參數(shù)定義了s參數(shù)解析操作成功的允許格式。它必須是NumberStyles枚舉中位標(biāo)志的組合。不支持以下NumberStyles成員:

  • NumberStyles.AllowHexSpecifier
  • NumberStyles.HexNumber

對(duì)provider指示的區(qū)域性,s參數(shù)可以包含PositiveInfinitySymbol、NegativeInfinitySymbolNaNSymbol。此外,根據(jù)style的值,s參數(shù)可能包括以下元素:

[ws] [$] [sign][integral-digits,]integral-digits[.fractional-digits][e[sign]exponential-digits][ws]

方括號(hào)([ 和 ])中的元素是可選的。下表描述了每個(gè)元素。

元素 描述
ws 可選的空白。如果style包含NumberStyles.AllowLeadingWhite標(biāo)志,則可以在s開(kāi)頭出現(xiàn)空格。如果style包含NumberStyles.AllowTrailingWhite標(biāo)志,它可以出現(xiàn)在s末尾。
$ 特定于文化的貨幣符號(hào)。它在字符串中的位置由provider參數(shù)的IFormatProvider.GetFormat方法返回的NumberFormatInfo對(duì)象的NumberFormatInfo.CurrencyNegativePatternNumberFormatInfo.CurrencyPositivePattern屬性定義。如果style包含NumberStyles.AllowCurrencySymbol標(biāo)志,則可以出現(xiàn)貨幣符號(hào)。
sign 可選標(biāo)志。如果style包含NumberStyles.AllowLeadingSign標(biāo)志,標(biāo)志可以出現(xiàn)在s開(kāi)頭,如果style包含NumberStyles.AllowTrailingSign標(biāo)志,它可以出現(xiàn)在s結(jié)尾。如果包含NumberStyles.AllowParentheses標(biāo)志,括號(hào)可以用在s中表示負(fù)值。
integral-digits 一系列從 0 到 9 的數(shù)字,用于指定數(shù)字的整數(shù)部分。如果有小數(shù)位,則可以不存在整數(shù)位。
, 特定于文化的千位分隔符。如果style包含NumberStyles.AllowThousands標(biāo)志,則s可以出現(xiàn)當(dāng)前區(qū)域性的千位分隔符。
. 特定于文化的小數(shù)點(diǎn)符號(hào)。如果style包含NumberStyles.AllowDecimalPoint標(biāo)志,則s可以出現(xiàn)當(dāng)前區(qū)域性的小數(shù)點(diǎn)符號(hào)。
fractional-digits 一系列從 0 到 9 的數(shù)字,用于指定數(shù)字的小數(shù)部分。如果style包含NumberStyles.AllowDecimalPoint標(biāo)志,則s可以出現(xiàn)小數(shù)位數(shù)。
e e 或 E 字符,表示s可以使用指數(shù)表示法表示數(shù)字。如果 style 包含NumberStyles.AllowExponent標(biāo)志,則s參數(shù)可以用指數(shù)表示法表示數(shù)字。
exponential-digits 一系列從 0 到 9 的數(shù)字,用于指定指數(shù)。

注:無(wú)論style參數(shù)的值如何,解析操作都會(huì)忽略s中的任何終止 NUL (U+0000) 字符。

僅包含數(shù)字的字符串(對(duì)應(yīng)于NumberStyles.None樣式)如果在Single類型的范圍內(nèi),則始終會(huì)成功解析。剩余的System.Globalization.NumberStyles成員控制元素可能但不是必須出現(xiàn)在輸入字符串中。下表顯示了各個(gè)NumberStyles標(biāo)志如何影響s可能存在的元素。

NumberStyles 值 s 中除數(shù)字外允許的元素
None 僅限整數(shù)位元素。
AllowDecimalPoint 點(diǎn)(.)和小數(shù)元素。
AllowExponent s參數(shù)也可以使用指數(shù)表示法。該標(biāo)志本身支持整數(shù)數(shù)字E指數(shù)數(shù)字形式的值;需要附加標(biāo)志才能成功解析具有正號(hào)或負(fù)號(hào)和小數(shù)點(diǎn)符號(hào)等元素的指數(shù)符號(hào)的字符串。
AllowLeadingWhite s開(kāi)頭的ws(空白符)元素。
AllowTrailingWhite s末尾的ws(空白符)元素。
AllowLeadingSign s開(kāi)頭的符號(hào)元素。
AllowTrailingSign s末尾的符號(hào)元素。
AllowParentheses 以括號(hào)形式包含數(shù)值的符號(hào)元素。
AllowThousands ,元素。
AllowCurrencySymbol $元素。
Currency 全部。s參數(shù)不能表示十六進(jìn)制數(shù)或指數(shù)表示法的數(shù)字。
Float ws元素在的s開(kāi)頭或結(jié)尾,符號(hào)在s的開(kāi)頭,以及.符號(hào)。s參數(shù)也可以使用指數(shù)表示法。
Number ,,ws,sign,千位分隔符 (,)和小數(shù)點(diǎn) ( . ) 元素。
Any 所有樣式,除了s不能表示十六進(jìn)制數(shù)。

provider參數(shù)是一個(gè)IFormatProvider實(shí)現(xiàn),其GetFormat方法返回一個(gè)NumberFormatInfo對(duì)象,該對(duì)象提供特定于區(qū)域性的格式信息。當(dāng)調(diào)用TryParse(String, NumberStyles, IFormatProvider, Single)方法時(shí),它會(huì)調(diào)用provider參數(shù)的GetFormat方法并向其傳遞一個(gè)表示NumberFormatInfo類型的Type對(duì)象。GetFormat方法然后返回NumberFormatInfo對(duì)象,該對(duì)象提供有關(guān)參數(shù)格式的信息。sprovider有三種使用方法為解析操作提供自定義格式信息的參數(shù):

  • 你可以傳遞一個(gè)CultureInfo對(duì)象,該對(duì)象表示提供格式信息的區(qū)域性。它的GetFormat方法返回提供該區(qū)域性的數(shù)字格式信息的NumberFormatInfo對(duì)象。
  • 你可以傳遞提供數(shù)字格式信息的實(shí)際NumberFormatInfo對(duì)象。(它的GetFormat實(shí)現(xiàn)只是返回自身。)
  • 你可以傳遞一個(gè)實(shí)現(xiàn)IFormatProvider的自定義對(duì)象。它的GetFormat方法實(shí)例化并返回提供格式信息的NumberFormatInfo對(duì)象。

如果providernull,則s根據(jù)當(dāng)前區(qū)域性的NumberFormatInfo對(duì)象解釋null的格式。

如果s超出Single數(shù)據(jù)類型的范圍,則該方法會(huì)在 .NET Framework 和 .NET Core 2.2 及更早版本上引發(fā)OverflowException 。在 .NET Core 3.0 及更高版本上,如果s小于Single.MinValue則返回Single.NegativeInfinity,如果s大于Single.MaxValue則返回Single.PositiveInfinity。

如果在解析操作過(guò)程中在s參數(shù)中遇到分隔符,并且適用的貨幣或數(shù)字小數(shù)和組分隔符相同,則解析操作假定分隔符是小數(shù)分隔符而不是組分隔符。有關(guān)分隔符的詳細(xì)信息,請(qǐng)參閱CurrencyDecimalSeparator、NumberDecimalSeparatorCurrencyGroupSeparatorNumberGroupSeparator。

總結(jié)

本文通過(guò)具體示例詳細(xì)介紹了TryParse(String, NumberStyles, IFormatProvider, Single)的用法,通過(guò)本文的學(xué)習(xí),我們應(yīng)該了解到如何將指定樣式和特定區(qū)域性格式的數(shù)字的字符串表示形式轉(zhuǎn)換為其等效的單精度浮點(diǎn)數(shù)。

參考文章

x
  • 站長(zhǎng)推薦
/* 左側(cè)顯示文章內(nèi)容目錄 */