Single.Parse メソッド

定義

数値の文字列形式を、それと等価な単精度浮動小数点数に変換します。

オーバーロード

Parse(String, NumberStyles, IFormatProvider)

指定したスタイルおよびカルチャに固有の書式による数値の文字列形式を、それと等価な単精度浮動小数点数に変換します。

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

指定したスタイルおよびカルチャに固有の書式による数値の文字列表現を含む文字スパンを、それと等価な単精度浮動小数点数に変換します。

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

UTF-8 文字のスパンを値に解析します。

Parse(String, IFormatProvider)

指定したカルチャに固有の書式による数値の文字列形式を、それと等価な単精度浮動小数点数に変換します。

Parse(String)

数値の文字列形式を、それと等価な単精度浮動小数点数に変換します。

Parse(ReadOnlySpan<Char>, IFormatProvider)

文字のスパンを値に解析します。

Parse(ReadOnlySpan<Byte>, IFormatProvider)

UTF-8 文字のスパンを値に解析します。

Parse(String, NumberStyles)

指定したスタイルでの数値の文字列形式を、それと等価な単精度浮動小数点数に変換します。

注釈

.NET Core 3.0 以降では、表すには大きすぎる値は、IEEE 754 仕様で必要に応じて または NegativeInfinityPositiveInfinity丸められます。 .NET Frameworkを含む以前のバージョンでは、大きすぎる値を解析するとエラーが発生しました。

Parse(String, NumberStyles, IFormatProvider)

Source:
Single.cs
Source:
Single.cs
Source:
Single.cs

指定したスタイルおよびカルチャに固有の書式による数値の文字列形式を、それと等価な単精度浮動小数点数に変換します。

public:
 static float Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider);
public:
 static float Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider) = System::Numerics::INumberBase<float>::Parse;
public static float Parse (string s, System.Globalization.NumberStyles style, IFormatProvider provider);
public static float Parse (string s, System.Globalization.NumberStyles style, IFormatProvider? provider);
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> single
Public Shared Function Parse (s As String, style As NumberStyles, provider As IFormatProvider) As Single

パラメーター

s
String

変換する数値を含んだ文字列。

style
NumberStyles

s で存在する可能性を持つスタイル要素を示す、列挙値のビットごとの組み合わせ。 通常指定する値は、AllowThousands と組み合わせた Float です。

provider
IFormatProvider

s に関するカルチャ固有の書式情報を提供するオブジェクト。

戻り値

s で指定した数値または記号に等しい単精度浮動小数点数。

実装

例外

snullです。

s が数値を表していません。

styleNumberStyles 値ではありません。

または

styleAllowHexSpecifier 値です。

.NET Framework および .NET Core 2.2 以前のバージョンのみ: sSingle.MinValue より小さい数値または Single.MaxValue より大きい数値を表します。

次のコード例では、 メソッドを Parse(String, NumberStyles, IFormatProvider) 使用して値の文字列表現を解析します Single 。 配列内の各文字列は、en-US、nl-NL、およびカスタム カルチャの書式設定規則を使用して解析されます。 カスタム カルチャでは、グループ区切り記号をアンダースコア ("_") として定義し、そのグループ サイズを 2 として定義します。

using System;
using System.Globalization;

public class Example
{
    public static void Main()
    {
      // Define an array of string values.
      string[] values = { " 987.654E-2", " 987,654E-2",  "(98765,43210)",
                          "9,876,543.210", "9.876.543,210",  "98_76_54_32,19" };
      // Create a custom culture based on the invariant culture.
      CultureInfo ci = new CultureInfo("");
      ci.NumberFormat.NumberGroupSizes = new int[] { 2 };
      ci.NumberFormat.NumberGroupSeparator = "_";

      // Define an array of format providers.
      CultureInfo[] providers = { new CultureInfo("en-US"),
                                  new CultureInfo("nl-NL"), ci };

      // Define an array of styles.
      NumberStyles[] styles = { NumberStyles.Currency, NumberStyles.Float };

      // Iterate the array of format providers.
      foreach (CultureInfo provider in providers)
      {
         Console.WriteLine("Parsing using the {0} culture:",
                           provider.Name == String.Empty ? "Invariant" : provider.Name);
         // Parse each element in the array of string values.
         foreach (string value in values)
         {
            foreach (NumberStyles style in styles)
            {
               try {
                  float number = Single.Parse(value, style, provider);
                  Console.WriteLine("   {0} ({1}) -> {2}",
                                    value, style, number);
               }
               catch (FormatException) {
                  Console.WriteLine("   '{0}' is invalid using {1}.", value, style);
               }
               catch (OverflowException) {
                  Console.WriteLine("   '{0}' is out of the range of a Single.", value);
               }
            }
         }
         Console.WriteLine();
      }
   }
}
// The example displays the following output:
// Parsing using the en-US culture:
//    ' 987.654E-2' is invalid using Currency.
//     987.654E-2 (Float) -> 9.87654
//    ' 987,654E-2' is invalid using Currency.
//    ' 987,654E-2' is invalid using Float.
//    (98765,43210) (Currency) -> -9.876543E+09
//    '(98765,43210)' is invalid using Float.
//    9,876,543.210 (Currency) -> 9876543
//    '9,876,543.210' is invalid using Float.
//    '9.876.543,210' is invalid using Currency.
//    '9.876.543,210' is invalid using Float.
//    '98_76_54_32,19' is invalid using Currency.
//    '98_76_54_32,19' is invalid using Float.
//
// Parsing using the nl-NL culture:
//    ' 987.654E-2' is invalid using Currency.
//    ' 987.654E-2' is invalid using Float.
//    ' 987,654E-2' is invalid using Currency.
//     987,654E-2 (Float) -> 9.87654
//    (98765,43210) (Currency) -> -98765.43
//    '(98765,43210)' is invalid using Float.
//    '9,876,543.210' is invalid using Currency.
//    '9,876,543.210' is invalid using Float.
//    9.876.543,210 (Currency) -> 9876543
//    '9.876.543,210' is invalid using Float.
//    '98_76_54_32,19' is invalid using Currency.
//    '98_76_54_32,19' is invalid using Float.
//
// Parsing using the Invariant culture:
//    ' 987.654E-2' is invalid using Currency.
//     987.654E-2 (Float) -> 9.87654
//    ' 987,654E-2' is invalid using Currency.
//    ' 987,654E-2' is invalid using Float.
//    (98765,43210) (Currency) -> -9.876543E+09
//    '(98765,43210)' is invalid using Float.
//    9,876,543.210 (Currency) -> 9876543
//    '9,876,543.210' is invalid using Float.
//    '9.876.543,210' is invalid using Currency.
//    '9.876.543,210' is invalid using Float.
//    98_76_54_32,19 (Currency) -> 9.876543E+09
//    '98_76_54_32,19' is invalid using Float.
open System
open System.Globalization

// Define a list of string values.
let values = 
    [ " 987.654E-2"; " 987,654E-2"; "(98765,43210)"
      "9,876,543.210"; "9.876.543,210"; "98_76_54_32,19" ]
// Create a custom culture based on the invariant culture.
let ci = CultureInfo ""
ci.NumberFormat.NumberGroupSizes <- [| 2 |]
ci.NumberFormat.NumberGroupSeparator <- "_"

// Define a list of format providers.
let providers = 
    [ CultureInfo "en-US"
      CultureInfo "nl-NL"
      ci ]

// Define a list of styles.
let styles = [ NumberStyles.Currency; NumberStyles.Float ]

// Iterate the list of format providers.
for provider in providers do
    printfn $"""Parsing using the {if provider.Name = String.Empty then "Invariant" else provider.Name} culture:"""
    // Parse each element in the array of string values.
    for value in values do
        for style in styles do
            try
                let number = Single.Parse(value, style, provider)
                printfn $"   {value} ({style}) -> {number}"
            with
            | :? FormatException ->
                printfn $"   '{value}' is invalid using {style}."
            | :? OverflowException ->
                printfn $"   '{value}' is out of the range of a Single."
    printfn ""

// The example displays the following output:
// Parsing using the en-US culture:
//    ' 987.654E-2' is invalid using Currency.
//     987.654E-2 (Float) -> 9.87654
//    ' 987,654E-2' is invalid using Currency.
//    ' 987,654E-2' is invalid using Float.
//    (98765,43210) (Currency) -> -9.876543E+09
//    '(98765,43210)' is invalid using Float.
//    9,876,543.210 (Currency) -> 9876543
//    '9,876,543.210' is invalid using Float.
//    '9.876.543,210' is invalid using Currency.
//    '9.876.543,210' is invalid using Float.
//    '98_76_54_32,19' is invalid using Currency.
//    '98_76_54_32,19' is invalid using Float.
//
// Parsing using the nl-NL culture:
//    ' 987.654E-2' is invalid using Currency.
//    ' 987.654E-2' is invalid using Float.
//    ' 987,654E-2' is invalid using Currency.
//     987,654E-2 (Float) -> 9.87654
//    (98765,43210) (Currency) -> -98765.43
//    '(98765,43210)' is invalid using Float.
//    '9,876,543.210' is invalid using Currency.
//    '9,876,543.210' is invalid using Float.
//    9.876.543,210 (Currency) -> 9876543
//    '9.876.543,210' is invalid using Float.
//    '98_76_54_32,19' is invalid using Currency.
//    '98_76_54_32,19' is invalid using Float.
//
// Parsing using the Invariant culture:
//    ' 987.654E-2' is invalid using Currency.
//     987.654E-2 (Float) -> 9.87654
//    ' 987,654E-2' is invalid using Currency.
//    ' 987,654E-2' is invalid using Float.
//    (98765,43210) (Currency) -> -9.876543E+09
//    '(98765,43210)' is invalid using Float.
//    9,876,543.210 (Currency) -> 9876543
//    '9,876,543.210' is invalid using Float.
//    '9.876.543,210' is invalid using Currency.
//    '9.876.543,210' is invalid using Float.
//    98_76_54_32,19 (Currency) -> 9.876543E+09
//    '98_76_54_32,19' is invalid using Float.
Imports System.Globalization

Module Example
    Public Sub Main()
      ' Define an array of string values.
      Dim values() As String = { " 987.654E-2", " 987,654E-2", _
                                 "(98765,43210)", "9,876,543.210",  _
                                 "9.876.543,210",  "98_76_54_32,19" }
      ' Create a custom culture based on the invariant culture.
      Dim ci As New CultureInfo("")
      ci.NumberFormat.NumberGroupSizes = New Integer() { 2 }
      ci.NumberFormat.NumberGroupSeparator = "_"
      
      ' Define an array of format providers.
      Dim providers() As CultureInfo = { New CultureInfo("en-US"), _
                                             New CultureInfo("nl-NL"), ci }       
      
      ' Define an array of styles.
      Dim styles() As NumberStyles = { NumberStyles.Currency, NumberStyles.Float }
      
      ' Iterate the array of format providers.
      For Each provider As CultureInfo In providers
         Console.WriteLine("Parsing using the {0} culture:", _
                           If(provider.Name = String.Empty, "Invariant", provider.Name))
         ' Parse each element in the array of string values.
         For Each value As String In values
            For Each style As NumberStyles In styles
               Try
                  Dim number As Single = Single.Parse(value, style, provider)            
                  Console.WriteLine("   {0} ({1}) -> {2}", _
                                    value, style, number)
               Catch e As FormatException
                  Console.WriteLine("   '{0}' is invalid using {1}.", value, style)            
               Catch e As OverflowException
                  Console.WriteLine("   '{0}' is out of the range of a Single.", value)
               End Try 
            Next            
         Next         
         Console.WriteLine()
      Next
   End Sub   
End Module 
' The example displays the following output:
'       Parsing using the en-US culture:
'          ' 987.654E-2' is invalid using Currency.
'           987.654E-2 (Float) -> 9.87654
'          ' 987,654E-2' is invalid using Currency.
'          ' 987,654E-2' is invalid using Float.
'          (98765,43210) (Currency) -> -9.876543E+09
'          '(98765,43210)' is invalid using Float.
'          9,876,543.210 (Currency) -> 9876543
'          '9,876,543.210' is invalid using Float.
'          '9.876.543,210' is invalid using Currency.
'          '9.876.543,210' is invalid using Float.
'          '98_76_54_32,19' is invalid using Currency.
'          '98_76_54_32,19' is invalid using Float.
'       
'       Parsing using the nl-NL culture:
'          ' 987.654E-2' is invalid using Currency.
'          ' 987.654E-2' is invalid using Float.
'          ' 987,654E-2' is invalid using Currency.
'           987,654E-2 (Float) -> 9.87654
'          (98765,43210) (Currency) -> -98765.43
'          '(98765,43210)' is invalid using Float.
'          '9,876,543.210' is invalid using Currency.
'          '9,876,543.210' is invalid using Float.
'          9.876.543,210 (Currency) -> 9876543
'          '9.876.543,210' is invalid using Float.
'          '98_76_54_32,19' is invalid using Currency.
'          '98_76_54_32,19' is invalid using Float.
'       
'       Parsing using the Invariant culture:
'          ' 987.654E-2' is invalid using Currency.
'           987.654E-2 (Float) -> 9.87654
'          ' 987,654E-2' is invalid using Currency.
'          ' 987,654E-2' is invalid using Float.
'          (98765,43210) (Currency) -> -9.876543E+09
'          '(98765,43210)' is invalid using Float.
'          9,876,543.210 (Currency) -> 9876543
'          '9,876,543.210' is invalid using Float.
'          '9.876.543,210' is invalid using Currency.
'          '9.876.543,210' is invalid using Float.
'          98_76_54_32,19 (Currency) -> 9.876543E+09
'          '98_76_54_32,19' is invalid using Float.

注釈

.NET Core 3.0 以降では、表すには大きすぎる値は、IEEE 754 仕様で必要に応じて または NegativeInfinityPositiveInfinity丸められます。 .NET Frameworkを含む以前のバージョンでは、大きすぎる値を解析するとエラーが発生しました。

パラメーターは style 、解析操作を成功させるために パラメーターで s 許可されるスタイル要素 (空白、桁区切り記号、通貨記号など) を定義します。 列挙体のビット フラグ NumberStyles の組み合わせである必要があります。 次 NumberStyles のメンバーはサポートされていません。

パラメーターにはs、 でprovider指定されたカルチャの 、NumberFormatInfo.NegativeInfinitySymbol、または NumberFormatInfo.NaNSymbol を含NumberFormatInfo.PositiveInfinitySymbolめることができます。 の style値に応じて、次の形式を使用することもできます。

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

角かっこ ([ と ]) で囲まれた要素は省略可能です。 次の表は、それぞれの要素の説明です。

要素 説明
ws 一連の空白文字。 空白は、 フラグを含む場合は のs先頭に表示され、フラグが含NumberStyles.AllowLeadingWhiteまれている場合styleは のs末尾にNumberStyles.AllowTrailingWhite表示styleされます。
$ カルチャ固有の通貨記号。 文字列内での位置は、現在のカルチャの NumberFormatInfo.CurrencyNegativePattern プロパティと NumberFormatInfo.CurrencyPositivePattern プロパティによって定義されます。 に フラグが含まれている場合styleは、現在のカルチャの通貨記号を NumberStyles.AllowCurrencySymbols表示できます。
sign 負符号記号 (-) または正符号記号 (+)。 署名は、 フラグを含む場合は のs先頭に表示され、フラグが含NumberStyles.AllowLeadingSignまれている場合styleは のs末尾にNumberStyles.AllowTrailingSign表示styleされます。 に フラグが含まれている場合styleは、かっこを使用sして負の値をNumberStyles.AllowParentheses示すことができます。
整数桁 数値の整数部分を指定する 0 から 9 までの一連の数字。 整数桁要素は、文字列に小数部の要素が含まれている場合は存在しない可能性があります。
, カルチャ固有のグループ区切り記号。 現在のカルチャのグループ区切り記号は、 に フラグが含まれている場合stylesNumberStyles.AllowThousands表示できます
. カルチャ固有の小数点記号。 に フラグが含まれている場合styleは、現在のカルチャの小数点記号を NumberStyles.AllowDecimalPoints表示できます。
小数部の桁数 数値の小数部を指定する 0 から 9 までの一連の数字。 フラグが含まれている場合style、小数部の数字が にsNumberStyles.AllowDecimalPoint表示されます。
E "e" または "E" 文字。値が指数 (指数) 表記で表されることを示します。 フラグが含まれている場合style、パラメーターはs指数表記で数値をNumberStyles.AllowExponent表すことができます。
exponential-digits 指数を指定する 0 ~ 9 の範囲の一連の数字。

Note

の終端の NUL (U+0000) 文字 s は、引数の style 値に関係なく、解析操作では無視されます。

数字のみの文字列 (スタイルに NumberStyles.None 対応) は、型の Single 範囲内にある場合は常に正常に解析されます。 残りの System.Globalization.NumberStyles メンバーは、入力文字列内に存在する可能性がありますが、存在する必要がない要素を制御します。 次の表は、 にs存在する可能性がある要素に個々NumberStylesのフラグがどのように影響するかを示しています。

NumberStyles 値 数字に加えて許可される s 要素
None 整数桁の要素のみ。
AllowDecimalPoint 小数点 (.) 要素と 小数部の要素
AllowExponent 指数表記を示す "e" または "E" 文字。 このフラグ自体では、E の形式の値がサポートされます。正符号や負符号、小数点記号などの要素を含む文字列を正常に解析するには、追加のフラグが必要です。
AllowLeadingWhite の先頭sにある ws 要素。
AllowTrailingWhite の末尾sにある ws 要素。
AllowLeadingSign の先頭sにある sign 要素。
AllowTrailingSign の末尾sにある sign 要素。
AllowParentheses 数値を囲むかっこの形式の sign 要素。
AllowThousands 桁区切り記号 (,) 要素。
AllowCurrencySymbol currency ($) 要素。
Currency すべての要素。 ただし、 s 16 進数または数値を指数表記で表すことはできません。
Float の先頭または末尾の sws 要素、の先頭のs符号、および小数点 (.) 記号。 パラメーターでは s 、指数表記を使用することもできます。
Number wssign、桁区切り記号 (,) および小数点 (.) 要素。
Any すべての要素。 ただし、 s 16 進数を表すことはできません。

パラメーターは provider 実装です IFormatProvider 。 そのメソッドは GetFormat 、 の形式に NumberFormatInfo 関するカルチャ固有の value情報を提供する オブジェクトを返します。 通常は、 provider 次のいずれかを指定できます。

  • CultureInfo数値書式情報を提供するカルチャを表す オブジェクト。 そのメソッドは GetFormat 、数値書式情報を NumberFormatInfo 提供する オブジェクトを返します。

  • NumberFormatInfo書式設定情報を提供する オブジェクト。 (の実装 GetFormat は、それ自体を返すだけです)。

  • メソッドを IFormatProvider 実装して使用して、書式設定情報を GetFormat 提供する オブジェクトを NumberFormatInfo インスタンス化して返すカスタム オブジェクト。

nullの場合providerは、現在のNumberFormatInfoカルチャの オブジェクトが使用されます。

がデータ型の範囲外のSingle場合s、メソッドは .NET Framework および .NET Core 2.2 以前のバージョンで をスローOverflowExceptionします。 .NET Core 3.0 以降のバージョンでは、 が よりSingle.MinValue小さい場合sは を返しSingle.PositiveInfinity、 が よりSingle.MaxValue大きい場合sは を返Single.NegativeInfinityします。

解析操作中にパラメーターで s 区切り記号が検出され、該当する通貨または数値の小数点とグループ区切り記号が同じである場合、解析操作では、区切り記号がグループ区切り記号ではなく小数点の区切り記号であると見なされます。 区切り記号の詳細については、「、、、および 」を参照してくださいCurrencyDecimalSeparatorCurrencyGroupSeparatorNumberDecimalSeparatorNumberGroupSeparator

こちらもご覧ください

適用対象

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

Source:
Single.cs
Source:
Single.cs
Source:
Single.cs

指定したスタイルおよびカルチャに固有の書式による数値の文字列表現を含む文字スパンを、それと等価な単精度浮動小数点数に変換します。

public static float Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.Float, IFormatProvider? provider = default);
public static float Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.Float, IFormatProvider provider = default);
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> single
Public Shared Function Parse (s As ReadOnlySpan(Of Char), Optional style As NumberStyles = System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.Float, Optional provider As IFormatProvider = Nothing) As Single

パラメーター

s
ReadOnlySpan<Char>

変換する数値を含む文字スパン。

style
NumberStyles

s で使用可能なスタイル要素を示す、列挙値のビットごとの組み合わせ。 通常指定する値は、AllowThousands と組み合わせた Float です。

provider
IFormatProvider

s に関するカルチャ固有の書式情報を提供するオブジェクト。

戻り値

s で指定した数値または記号に等しい単精度浮動小数点数。

実装

例外

s が数値を表していません。

styleNumberStyles 値ではありません。

または

styleAllowHexSpecifier 値です。

注釈

.NET Core 3.0 以降では、表すには大きすぎる値は、IEEE 754 仕様で必要に応じて または NegativeInfinityPositiveInfinity丸められます。 .NET Frameworkを含む以前のバージョンでは、大きすぎる値を解析するとエラーが発生しました。

がデータ型の範囲外の場合s、 が よりSingle.MinValue小さい場合sは を返しSingle.PositiveInfinity、 が よりSingle.MaxValue大きい場合sは を返Single.NegativeInfinitySingleします。

適用対象

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

Source:
Single.cs
Source:
Single.cs

UTF-8 文字のスパンを値に解析します。

public static float Parse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.Float, IFormatProvider? provider = default);
static member Parse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider -> single
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), Optional style As NumberStyles = System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.Float, Optional provider As IFormatProvider = Nothing) As Single

パラメーター

utf8Text
ReadOnlySpan<Byte>

解析する UTF-8 文字のスパン。

style
NumberStyles

utf8Text存在できる数値スタイルのビットごとの組み合わせ。

provider
IFormatProvider

utf8Text に関するカルチャ固有の書式情報を提供するオブジェクト。

戻り値

を解析した utf8Text結果。

実装

適用対象

Parse(String, IFormatProvider)

Source:
Single.cs
Source:
Single.cs
Source:
Single.cs

指定したカルチャに固有の書式による数値の文字列形式を、それと等価な単精度浮動小数点数に変換します。

public:
 static float Parse(System::String ^ s, IFormatProvider ^ provider);
public:
 static float Parse(System::String ^ s, IFormatProvider ^ provider) = IParsable<float>::Parse;
public static float Parse (string s, IFormatProvider provider);
public static float Parse (string s, IFormatProvider? provider);
static member Parse : string * IFormatProvider -> single
Public Shared Function Parse (s As String, provider As IFormatProvider) As Single

パラメーター

s
String

変換する数値を含んだ文字列。

provider
IFormatProvider

s に関するカルチャ固有の書式情報を提供するオブジェクト。

戻り値

s で指定した数値または記号に等しい単精度浮動小数点数。

実装

例外

snullです。

s は有効な形式で数値を表していません。

.NET Framework および .NET Core 2.2 以前のバージョンのみ: sは、Single.MinValue より小さい数値または Single.MaxValue より大きい数値を表します。

次の例は、Web フォームのボタン クリック イベント ハンドラーです。 プロパティによって返される配列を HttpRequest.UserLanguages 使用して、ユーザーのロケールを決定します。 その後、そのロケールに CultureInfo 対応する オブジェクトをインスタンス化します。 NumberFormatInfoそのCultureInfoオブジェクトに属する オブジェクトが メソッドにParse(String, IFormatProvider)渡され、ユーザーの入力が値にSingle変換されます。

protected void OkToSingle_Click(object sender, EventArgs e)
{
    string locale;
    float 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 = Single.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();
}
Protected Sub OkToSingle_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OkToSingle.Click
   Dim locale As String
   Dim culture As CultureInfo
   Dim number As Single

   ' Return if string is empty
   If String.IsNullOrEmpty(Me.inputNumber.Text) Then Exit Sub

   ' Get locale of web request to determine possible format of number
   If Request.UserLanguages.Length = 0 Then Exit Sub
   locale = Request.UserLanguages(0)
   If String.IsNullOrEmpty(locale) Then Exit Sub

   ' Instantiate CultureInfo object for the user's locale
   culture = New CultureInfo(locale)

   ' Convert user input from a string to a number
   Try
      number = Single.Parse(Me.inputNumber.Text, culture.NumberFormat)
   Catch ex As FormatException
      Exit Sub
   Catch ex As OverflowException
      Exit Sub
   End Try

   ' Output number to label on web form
   Me.outputNumber.Text = "Number is " & number.ToString()
End Sub

注釈

.NET Core 3.0 以降では、表すには大きすぎる値は、IEEE 754 仕様で必要に応じて または NegativeInfinityPositiveInfinity丸められます。 .NET Frameworkを含む以前のバージョンでは、大きすぎる値を解析するとエラーが発生しました。

このオーバーロードは、通常、さまざまな方法で書式設定できるテキストを値に変換するために Single 使用されます。 たとえば、ユーザーが入力したテキストを HTML テキスト ボックスに数値に変換するために使用できます。

パラメーターはs、 フラグと NumberStyles.AllowThousands フラグのNumberStyles.Float組み合わせを使用して解釈されます。 パラメーターにはs、 で指定されたproviderカルチャの 、NumberFormatInfo.NegativeInfinitySymbol、または NumberFormatInfo.NaNSymbol を含NumberFormatInfo.PositiveInfinitySymbolめることができます。または、次の形式の文字列を含めることができます。

[ws][sign]integral-digits[.[小数部]][E[sign]exponential-digits][ws]

省略可能な要素は、角かっこ ([ と ]) で囲まれます。 "digits" という用語を含む要素は、0 から 9 までの一連の数字で構成されます。

要素 説明
ws 一連の空白文字。
sign 負符号記号 (-) または正符号記号 (+)。
整数桁 数値の整数部分を指定する 0 から 9 までの一連の数字。 整数桁の実行は、グループ区切り記号でパーティション分割できます。 たとえば、一部のカルチャでは、コンマ (,) は数千のグループを区切ります。 整数桁要素は、文字列に小数部の要素が含まれている場合は存在しない可能性があります。
. カルチャ固有の小数点記号。
小数部の桁数 数値の小数部を指定する 0 から 9 までの一連の数字。
E "e" または "E" 文字。値が指数 (指数) 表記で表されることを示します。
exponential-digits 指数を指定する 0 ~ 9 の範囲の一連の数字。

数値書式の詳細については、「 書式の種類 」トピックを参照してください。

パラメーターはprovider、カルチャ固有のIFormatProviderGetFormat書式設定情報を提供する オブジェクトをNumberFormatInfoメソッドが返す実装です。 メソッドがParse(String, IFormatProvider)呼び出されると、パラメーターの GetFormat メソッドをprovider呼び出し、型をType表すオブジェクトをNumberFormatInfo渡します。 メソッドは GetFormat 、パラメーターの形式に NumberFormatInfo 関する情報を提供する オブジェクトを s 返します。 パラメーターを使用 provider して、解析操作にカスタム書式情報を指定するには、次の 3 つの方法があります。

  • 書式設定情報を CultureInfo 提供するカルチャを表す オブジェクトを渡すことができます。 そのメソッドは GetFormat 、そのカルチャの NumberFormatInfo 数値書式情報を提供する オブジェクトを返します。

  • 数値書式情報を提供する実際 NumberFormatInfo のオブジェクトを渡すことができます。 (の実装 GetFormat は、それ自体を返すだけです)。

  • を実装 IFormatProviderするカスタム オブジェクトを渡すことができます。 そのメソッドは GetFormat 、書式設定情報を提供する オブジェクトを NumberFormatInfo インスタンス化して返します。

null または をNumberFormatInfo取得できない場合providerは、現在のシステム カルチャの書式設定情報が使用されます。

がデータ型の範囲外のSingle場合s、メソッドは .NET Framework および .NET Core 2.2 以前のバージョンで をスローOverflowExceptionします。 .NET Core 3.0 以降のバージョンでは、 が よりSingle.MinValue小さい場合sは を返しSingle.PositiveInfinity、 が よりSingle.MaxValue大きい場合sは を返Single.NegativeInfinityします。

解析操作中にパラメーターで s 区切り記号が検出され、該当する通貨または数値の小数点とグループ区切り記号が同じである場合、解析操作では、区切り記号がグループ区切り記号ではなく小数点の区切り記号であると見なされます。 区切り記号の詳細については、「、、、および 」を参照してくださいCurrencyDecimalSeparatorCurrencyGroupSeparatorNumberDecimalSeparatorNumberGroupSeparator

の例 s としては、"100"、"-123,456,789"、"123.45e+6"、"+500"、"5e2"、"3.1416"、"600."、"-.123"、"-Infinity" があります。

こちらもご覧ください

適用対象

Parse(String)

Source:
Single.cs
Source:
Single.cs
Source:
Single.cs

数値の文字列形式を、それと等価な単精度浮動小数点数に変換します。

public:
 static float Parse(System::String ^ s);
public static float Parse (string s);
static member Parse : string -> single
Public Shared Function Parse (s As String) As Single

パラメーター

s
String

変換する数値を含んだ文字列。

戻り値

s で指定した数値または記号に等しい単精度浮動小数点数。

例外

snullです。

s は有効な形式で数値を表していません。

.NET Framework および .NET Core 2.2 以前のバージョンのみ: sは、Single.MinValue より小さい数値または Single.MaxValue より大きい数値を表します。

次の例では、 メソッドを Parse(String) 使用して、文字列の配列を同等 Single の値に変換します。

using System;

public class Example
{
   public static void Main()
   {
      string[] values = { "100", "(100)", "-123,456,789", "123.45e+6", 
                          "+500", "5e2", "3.1416", "600.", "-.123", 
                          "-Infinity", "-1E-16", Double.MaxValue.ToString(), 
                          Single.MinValue.ToString(), String.Empty };
      foreach (string value in values)
      {
         try {   
            float number = Single.Parse(value);
            Console.WriteLine("{0} -> {1}", value, number);
         }
         catch (FormatException) {
            Console.WriteLine("'{0}' is not in a valid format.", value);
         }
         catch (OverflowException) {
            Console.WriteLine("{0} is outside the range of a Single.", value);
         }
      }                                  
   }
}
// The example displays the following output:
//       100 -> 100
//       '(100)' is not in a valid format.
//       -123,456,789 -> -1.234568E+08
//       123.45e+6 -> 1.2345E+08
//       +500 -> 500
//       5e2 -> 500
//       3.1416 -> 3.1416
//       600. -> 600
//       -.123 -> -0.123
//       -Infinity -> -Infinity
//       -1E-16 -> -1E-16
//       1.79769313486232E+308 is outside the range of a Single.
//       -3.402823E+38 -> -3.402823E+38
//       '' is not in a valid format.
open System

let values = 
    [| "100"; "(100)"; "-123,456,789"; "123.45e+6" 
       "+500"; "5e2"; "3.1416"; "600."; "-.123" 
       "-Infinity"; "-1E-16"; string Double.MaxValue
       string Single.MinValue; String.Empty |]

for value in values do
    try
        let number = Single.Parse value
        printfn $"{value} -> {number}"
    with
    | :? FormatException ->
        printfn $"'{value}' is not in a valid format."
    | :? OverflowException ->
        printfn $"{value} is outside the range of a Single."
// The example displays the following output:
//       100 -> 100
//       '(100)' is not in a valid format.
//       -123,456,789 -> -1.234568E+08
//       123.45e+6 -> 1.2345E+08
//       +500 -> 500
//       5e2 -> 500
//       3.1416 -> 3.1416
//       600. -> 600
//       -.123 -> -0.123
//       -Infinity -> -Infinity
//       -1E-16 -> -1E-16
//       1.79769313486232E+308 is outside the range of a Single.
//       -3.402823E+38 -> -3.402823E+38
//       '' is not in a valid format.
Module Example
   Public Sub Main()
      Dim values() As String = { "100", "(100)", "-123,456,789", "123.45e+6", _
                                 "+500", "5e2", "3.1416", "600.", "-.123", _
                                 "-Infinity", "-1E-16", Double.MaxValue.ToString(), _
                                 Single.MinValue.ToString(), String.Empty }
      For Each value As String In values
         Try   
            Dim number As Single = Single.Parse(value)
            Console.WriteLine("{0} -> {1}", value, number)
         Catch e As FormatException
            Console.WriteLine("'{0}' is not in a valid format.", value)
         Catch e As OverflowException
            Console.WriteLine("{0} is outside the range of a Single.", value)
         End Try
      Next                                  
   End Sub
End Module
' The example displays the following output:
'       100 -> 100
'       '(100)' is not in a valid format.
'       -123,456,789 -> -1.234568E+08
'       123.45e+6 -> 1.2345E+08
'       +500 -> 500
'       5e2 -> 500
'       3.1416 -> 3.1416
'       600. -> 600
'       -.123 -> -0.123
'       -Infinity -> -Infinity
'       -1E-16 -> -1E-16
'       1.79769313486232E+308 is outside the range of a Single.
'       -3.402823E+38 -> -3.402823E+38
'       '' is not in a valid format.

注釈

.NET Core 3.0 以降では、表すには大きすぎる値は、IEEE 754 仕様で必要に応じて または NegativeInfinityPositiveInfinity丸められます。 .NET Frameworkを含む以前のバージョンでは、大きすぎる値を解析するとエラーが発生しました。

パラメーターには s 、現在のカルチャの PositiveInfinitySymbolNegativeInfinitySymbolNaNSymbol、または形式の文字列を含めることができます。

[ws][sign][整数桁[,]]integral-digits[.[小数部]][e[sign]exponential-digits][ws]

角かっこ ([ および ]) 内の要素は省略可能です。 次の表は、それぞれの要素の説明です。

要素 説明
ws 一連の空白文字。
sign 負符号記号または正符号記号。 有効な符号文字は、現在のカルチャの NumberFormatInfo.NegativeSign プロパティと NumberFormatInfo.PositiveSign プロパティによって決まります。 先頭の記号のみを使用できます。
整数桁 数値の整数部分を指定する 0 から 9 までの一連の数字。 整数桁の実行は、グループ区切り記号でパーティション分割できます。 たとえば、一部のカルチャでは、コンマ (,) は数千のグループを区切ります。 整数桁要素は、文字列に小数部の要素が含まれている場合は存在しない可能性があります。
, カルチャ固有の桁区切り記号。
. カルチャ固有の小数点記号。
小数部の桁数 数値の小数部を指定する 0 から 9 までの一連の数字。
E "e" または "E" 文字。値が指数 (指数) 表記で表されることを示します。
exponential-digits 指数を指定する 0 ~ 9 の範囲の一連の数字。

パラメーターはs、 フラグと NumberStyles.AllowThousands フラグのNumberStyles.Float組み合わせを使用して解釈されます。 つまり、空白と桁区切り記号は使用できますが、通貨記号は使用できません。 に存在できる要素 (通貨記号、桁区切り記号、空白など) を明示的に s定義するには、 メソッドのオーバーロードを Parse(String, NumberStyles) 使用します。

パラメーターは s 、現在のシステム カルチャ用に初期化された オブジェクトの NumberFormatInfo 書式設定情報を使用して解析されます。 詳細については、「CurrentInfo」を参照してください。 特定のカルチャの書式設定情報を使用して文字列を解析するには、 メソッドまたは Parse(String, NumberStyles, IFormatProvider) メソッドをParse(String, IFormatProvider)使用します。

通常、メソッドを Parse 呼び出して作成された文字列をメソッドに ToString 渡すと、元 Single の値が返されます。 ただし、精度が失われるため、値が等しくない可能性があります。

がデータ型の範囲外のSingle場合s、メソッドは .NET Framework および .NET Core 2.2 以前のバージョンで をスローOverflowExceptionします。 .NET Core 3.0 以降のバージョンでは、 が よりSingle.MinValue小さい場合sは を返しSingle.PositiveInfinity、 が よりSingle.MaxValue大きい場合sは を返Single.NegativeInfinityします。

解析操作中にパラメーターで s 区切り記号が検出され、該当する通貨または数値の小数点とグループ区切り記号が同じである場合、解析操作では、区切り記号がグループ区切り記号ではなく小数点の区切り記号であると見なされます。 区切り記号の詳細については、「、、、および 」を参照してくださいCurrencyDecimalSeparatorCurrencyGroupSeparatorNumberDecimalSeparatorNumberGroupSeparator

こちらもご覧ください

適用対象

Parse(ReadOnlySpan<Char>, IFormatProvider)

Source:
Single.cs
Source:
Single.cs
Source:
Single.cs

文字のスパンを値に解析します。

public:
 static float Parse(ReadOnlySpan<char> s, IFormatProvider ^ provider) = ISpanParsable<float>::Parse;
public static float Parse (ReadOnlySpan<char> s, IFormatProvider? provider);
static member Parse : ReadOnlySpan<char> * IFormatProvider -> single
Public Shared Function Parse (s As ReadOnlySpan(Of Char), provider As IFormatProvider) As Single

パラメーター

s
ReadOnlySpan<Char>

解析する文字のスパン。

provider
IFormatProvider

s に関するカルチャ固有の書式情報を提供するオブジェクト。

戻り値

を解析した s結果。

実装

適用対象

Parse(ReadOnlySpan<Byte>, IFormatProvider)

Source:
Single.cs
Source:
Single.cs

UTF-8 文字のスパンを値に解析します。

public:
 static float Parse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider) = IUtf8SpanParsable<float>::Parse;
public static float Parse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider);
static member Parse : ReadOnlySpan<byte> * IFormatProvider -> single
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider) As Single

パラメーター

utf8Text
ReadOnlySpan<Byte>

解析する UTF-8 文字のスパン。

provider
IFormatProvider

utf8Text に関するカルチャ固有の書式情報を提供するオブジェクト。

戻り値

を解析した utf8Text結果。

実装

適用対象

Parse(String, NumberStyles)

Source:
Single.cs
Source:
Single.cs
Source:
Single.cs

指定したスタイルでの数値の文字列形式を、それと等価な単精度浮動小数点数に変換します。

public:
 static float Parse(System::String ^ s, System::Globalization::NumberStyles style);
public static float Parse (string s, System.Globalization.NumberStyles style);
static member Parse : string * System.Globalization.NumberStyles -> single
Public Shared Function Parse (s As String, style As NumberStyles) As Single

パラメーター

s
String

変換する数値を含んだ文字列。

style
NumberStyles

s で存在する可能性を持つスタイル要素を示す、列挙値のビットごとの組み合わせ。 通常指定する値は、AllowThousands と組み合わせた Float です。

戻り値

s で指定した数値または記号に等しい単精度浮動小数点数。

例外

snullです。

s は有効な形式の数値ではありません。

.NET Framework および .NET Core 2.2 以前のバージョンのみ: sSingle.MinValue より小さい数値または Single.MaxValue より大きい数値を表します。

styleNumberStyles 値ではありません。

または

style には値 AllowHexSpecifier が含まれています。

次の例では、 メソッドを Parse(String, NumberStyles) 使用して値の文字列表現を解析します Single 。 この例では、en-US カルチャの書式設定情報を使用します。

using System;
using System.Globalization;
using System.Threading;

public class ParseString
{
   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)
   {
      Single number;
      try
      {
         number = Single.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();                           
   }   
}
// The example displays the following output to the console:
//    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.
open System
open System.Globalization
open System.Threading

let showNumericValue value (styles: NumberStyles) =
    try
        let number = Single.Parse(value, styles)
        printfn $"Converted '{value}' using {styles} to {number}."
    with :? FormatException ->
        printfn $"Unable to parse '{value}' with styles {styles}."
    printfn ""

[<EntryPoint>]
let main _ =
    // Set current thread culture to en-US.
    Thread.CurrentThread.CurrentCulture <- CultureInfo.CreateSpecificCulture "en-US"
    
    // Parse a string in exponential notation with only the AllowExponent flag. 
    let value = "-1.063E-02"
    let styles = NumberStyles.AllowExponent
    showNumericValue value styles
    
    // Parse a string in exponential notation
    // with the AllowExponent and Number flags.
    let 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.
    let value = " $ 6,164.3299  "
    let styles = NumberStyles.Number ||| NumberStyles.AllowCurrencySymbol
    showNumericValue value styles
    
    // Parse negative value with thousands separator and decimal.
    let value = "(4,320.64)"
    let styles = NumberStyles.AllowParentheses ||| NumberStyles.AllowTrailingSign ||| NumberStyles.Float 
    showNumericValue value styles
    
    let styles = NumberStyles.AllowParentheses ||| NumberStyles.AllowTrailingSign ||| NumberStyles.Float ||| NumberStyles.AllowThousands
    showNumericValue value styles
    0
// The example displays the following output to the console:
//    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.
Imports System.Globalization
Imports System.Threading

Module ParseStrings
   Public Sub Main()
      ' Set current thread culture to en-US.
      Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US")
            
      Dim value As String
      Dim styles As NumberStyles
      
      ' 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 Or 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 Or NumberStyles.AllowCurrencySymbol
      ShowNumericValue(value, styles)
      
      ' Parse negative value with thousands separator and decimal.
      value = "(4,320.64)"
      styles = NumberStyles.AllowParentheses Or NumberStyles.AllowTrailingSign _
               Or NumberStyles.Float 
      ShowNumericValue(value, styles)
      
      styles = NumberStyles.AllowParentheses Or NumberStyles.AllowTrailingSign _
               Or NumberStyles.Float Or NumberStyles.AllowThousands
      ShowNumericValue(value, styles)
   End Sub
   
   Private Sub ShowNumericValue(value As String, styles As NumberStyles)
      Dim number As Single
      Try
         number = Single.Parse(value, styles)
         Console.WriteLine("Converted '{0}' using {1} to {2}.", _
                           value, styles.ToString(), number)
      Catch e As FormatException
         Console.WriteLine("Unable to parse '{0}' with styles {1}.", _
                           value, styles.ToString())
      End Try
      Console.WriteLine()                           
   End Sub
End Module
' The example displays the following output to the console:
'    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.

注釈

.NET Core 3.0 以降では、表すには大きすぎる値は、IEEE 754 仕様で必要に応じて または NegativeInfinityPositiveInfinity丸められます。 .NET Frameworkを含む以前のバージョンでは、大きすぎる値を解析するとエラーが発生しました。

パラメーターは style 、解析操作を成功させるために パラメーターで s 許可されるスタイル要素 (空白、桁区切り記号、通貨記号など) を定義します。 列挙体のビット フラグ NumberStyles の組み合わせである必要があります。 次 NumberStyles のメンバーはサポートされていません。

パラメーターにはs、現在のカルチャの PositiveInfinitySymbol、、 NegativeInfinitySymbolNaNSymbolを含めることができます。 の style値に応じて、次の形式を使用することもできます。

[ws][$][sign][integral-digits[,]]integral-digits[.[小数部]][E[sign]exponential-digits][ws]

角かっこ ([ および ]) 内の要素は省略可能です。 次の表は、それぞれの要素の説明です。

Ws 一連の空白文字。 空白は、 フラグを含む場合は のs先頭に表示され、フラグが含NumberStyles.AllowLeadingWhiteまれている場合styleは のs末尾にNumberStyles.AllowTrailingWhite表示styleされます。

$ カルチャ固有の通貨記号。 文字列内での位置は、現在のカルチャの NumberFormatInfo.CurrencyNegativePattern プロパティと NumberFormatInfo.CurrencyPositivePattern プロパティによって定義されます。 に フラグが含まれている場合styleは、現在のカルチャの通貨記号を NumberStyles.AllowCurrencySymbols表示できます。

署名 負符号記号 (-) または正符号記号 (+)。 署名は、 フラグを含む場合は のs先頭に表示され、フラグが含NumberStyles.AllowLeadingSignまれている場合styleは のs末尾にNumberStyles.AllowTrailingSign表示styleされます。 に フラグが含まれている場合styleは、かっこを使用sして負の値をNumberStyles.AllowParentheses示すことができます。

整数桁 数値の整数部分を指定する 0 から 9 までの一連の数字。 整数桁要素は、文字列に小数部の要素が含まれている場合は存在しない可能性があります。

、カルチャ固有のグループ区切り記号。 現在のカルチャのグループ区切り記号は、 に フラグが含まれている場合stylesNumberStyles.AllowThousands表示できます

. カルチャ固有の小数点記号。 に フラグが含まれている場合styleは、現在のカルチャの小数点記号を NumberStyles.AllowDecimalPoints表示できます。

小数部の桁数 数値の小数部を指定する 0 から 9 までの一連の数字。 フラグが含まれている場合style、小数部の数字が にsNumberStyles.AllowDecimalPoint表示されます。

E 値が指数 (指数) 表記で表されることを示す "e" または "E" 文字。 フラグが含まれている場合style、パラメーターはvalue指数表記で数値をNumberStyles.AllowExponent表すことができます。

exponential-digits 指数を指定する 0 ~ 9 の範囲の一連の数字。

Note

の終端の NUL (U+0000) 文字 s は、引数の style 値に関係なく、解析操作では無視されます。

数字のみの文字列 (スタイルに NumberStyles.None 対応) は、型の Single 範囲内にある場合は常に正常に解析されます。 残りの System.Globalization.NumberStyles メンバーは、入力文字列内に存在する可能性がありますが、存在する必要がない要素を制御します。 次の表は、 にs存在する可能性がある要素に個々NumberStylesのフラグがどのように影響するかを示しています。

NumberStyles 値 数字に加えて許可される s 要素
None 整数桁の要素のみ。
AllowDecimalPoint 小数点 (.) 要素と 小数部の要素
AllowExponent 指数表記を示す "e" または "E" 文字。 このフラグ自体では、E の形式の値がサポートされます。正符号や負符号、小数点記号などの要素を含む文字列を正常に解析するには、追加のフラグが必要です。
AllowLeadingWhite の先頭sにある ws 要素。
AllowTrailingWhite の末尾sにある ws 要素。
AllowLeadingSign の先頭sにある sign 要素。
AllowTrailingSign の末尾sにある sign 要素。
AllowParentheses 数値を囲むかっこの形式の sign 要素。
AllowThousands 桁区切り記号 (,) 要素。
AllowCurrencySymbol currency ($) 要素。
Currency すべての要素。 ただし、 s 16 進数または数値を指数表記で表すことはできません。
Float の先頭または末尾の sws 要素、の先頭のs符号、および小数点 (.) 記号。 パラメーターでは s 、指数表記を使用することもできます。
Number wssign、桁区切り記号 (,) および小数点 (.) 要素。
Any すべての要素。 ただし、 s 16 進数を表すことはできません。

の例 s としては、"100"、"-123,456,789"、"123.45e+6"、"+500"、"5e2"、"3.1416"、"600."、"-.123"、"-Infinity" があります。

パラメーターは s 、現在のシステム カルチャ用に初期化された オブジェクトの NumberFormatInfo 書式設定情報を使用して解析されます。 解析操作に書式設定情報を使用するカルチャを指定するには、 オーバーロードを Parse(String, NumberStyles, IFormatProvider) 呼び出します。

通常、メソッドを Parse 呼び出して作成された文字列をメソッドに ToString 渡すと、元 Single の値が返されます。 ただし、精度が失われるため、値が等しくない可能性があります。

がデータ型の範囲外のSingle場合s、メソッドは .NET Framework および .NET Core 2.2 以前のバージョンで をスローOverflowExceptionします。 .NET Core 3.0 以降のバージョンでは、 が よりSingle.MinValue小さい場合sは を返しSingle.PositiveInfinity、 が よりSingle.MaxValue大きい場合sは を返Single.NegativeInfinityします。

解析操作中にパラメーターで s 区切り記号が検出され、該当する通貨または数値の小数点とグループ区切り記号が同じである場合、解析操作では、区切り記号がグループ区切り記号ではなく小数点の区切り記号であると見なされます。 区切り記号の詳細については、「、、、および 」を参照してくださいCurrencyDecimalSeparatorCurrencyGroupSeparatorNumberDecimalSeparatorNumberGroupSeparator

こちらもご覧ください

適用対象