次の方法で共有


Single.Parse メソッド

定義

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

オーバーロード

名前 説明
Parse(String)

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

Parse(ReadOnlySpan<Byte>, IFormatProvider)

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

Parse(ReadOnlySpan<Char>, IFormatProvider)

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

Parse(String, NumberStyles)

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

Parse(String, IFormatProvider)

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

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

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

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

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

Parse(String, NumberStyles, IFormatProvider)

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

注釈

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

Parse(String)

ソース:
Single.cs
ソース:
Single.cs
ソース:
Single.cs
ソース:
Single.cs
ソース:
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 のみ: 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 仕様で必要に応じて、大きすぎる値は PositiveInfinity または NegativeInfinity に丸められます。 以前のバージョン (.NET Framework を含む) では、大きすぎる値を解析するとエラーが発生しました。

s パラメーターには、現在のカルチャのPositiveInfinitySymbolNegativeInfinitySymbol、またはNaNSymbolシンボルを含めることができます。 この文字列比較は、.NET Core 3.0 以降のバージョンでは大文字と小文字は区別されませんが、.NET Framework を含む以前のバージョンでは大文字と小文字が区別されます。 s パラメーターには、次の形式の文字列を指定することもできます。

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

角かっこ ([ と ]) の要素は省略可能です。 次の表では、各要素について説明します。

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

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

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

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

sSingleデータ型の範囲外の場合、メソッドは .NET Framework でOverflowExceptionをスローします。 .NET Core 3.0 以降のバージョンでは、Single.NegativeInfinitys未満の場合はSingle.MinValueを返し、Single.PositiveInfinitysより大きい場合はSingle.MaxValueします。

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

こちらもご覧ください

適用対象

Parse(ReadOnlySpan<Byte>, IFormatProvider)

ソース:
Single.cs
ソース:
Single.cs
ソース:
Single.cs
ソース:
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(ReadOnlySpan<Char>, IFormatProvider)

ソース:
Single.cs
ソース:
Single.cs
ソース:
Single.cs
ソース:
Single.cs
ソース:
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(String, NumberStyles)

ソース:
Single.cs
ソース:
Single.cs
ソース:
Single.cs
ソース:
Single.cs
ソース:
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に存在できるスタイル要素を示す列挙値のビットごとの組み合わせ。 指定する一般的な値は、Floatと組み合わせてAllowThousands

返品

sで指定された数値または記号に相当する単精度浮動小数点数。

例外

snullです。

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

.NET Framework のみ: s、Single.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 仕様で必要に応じて、大きすぎる値は PositiveInfinity または NegativeInfinity に丸められます。 以前のバージョン (.NET Framework を含む) では、大きすぎる値を解析するとエラーが発生しました。

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

s パラメーターには、現在のカルチャのPositiveInfinitySymbolNegativeInfinitySymbol、またはNaNSymbolシンボルを含めることができます。 この文字列比較は、.NET Core 3.0 以降のバージョンでは大文字と小文字は区別されませんが、.NET Framework を含む以前のバージョンでは大文字と小文字が区別されます。 styleの値に応じて、s パラメーターは次の形式をとることもできます。

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

角かっこ ([ と ]) の要素は省略可能です。 次の表では、各要素について説明します。

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

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

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

整数桁 数値の整数部分を指定する 0 から 9 までの一連の数字。 整数桁の要素は、文字列に小数部の要素が含まれている場合は使用できません。

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

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

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

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

exponential-digits 指数を指定する 0 から 9 までの一連の数字。

手記

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

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

NumberStyles 値 数字に加えて s で許可される要素
None 整数桁の要素のみ。
AllowDecimalPoint 小数点 (.) 要素と 小数部の桁数 の要素。
AllowExponent 指数表記を示す "e" または "E" 文字。 このフラグ自体は、フォーム の数字 E 桁の値をサポートします。正または負の符号や小数点記号などの要素を使用して文字列を正常に解析するには、追加のフラグが必要です。
AllowLeadingWhite の先頭にある s 要素。
AllowTrailingWhite の末尾にある s 要素。
AllowLeadingSign の先頭にある s 要素。
AllowTrailingSign の末尾にある s 要素。
AllowParentheses 数値を囲むかっこの形式の 符号 要素。
AllowThousands 桁区切り記号 (,) 要素。
AllowCurrencySymbol currency ($) 要素。
Currency すべての要素。 ただし、 s は、指数表記で 16 進数または数値を表すことはできません。
Float の先頭または末尾の s 要素、の先頭の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値が返されます。 ただし、精度が失われるため、値が等しくない可能性があります。

sSingleデータ型の範囲外の場合、メソッドは .NET Framework でOverflowExceptionをスローします。 .NET Core 3.0 以降のバージョンでは、Single.NegativeInfinitys未満の場合はSingle.MinValueを返し、Single.PositiveInfinitysより大きい場合はSingle.MaxValueします。

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

こちらもご覧ください

適用対象

Parse(String, IFormatProvider)

ソース:
Single.cs
ソース:
Single.cs
ソース:
Single.cs
ソース:
Single.cs
ソース:
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 のみ: 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 仕様で必要に応じて、大きすぎる値は PositiveInfinity または NegativeInfinity に丸められます。 以前のバージョン (.NET Framework を含む) では、大きすぎる値を解析するとエラーが発生しました。

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

s パラメーターは、NumberStyles.FloatフラグとNumberStyles.AllowThousands フラグの組み合わせを使用して解釈されます。 s パラメーターには、NumberFormatInfo.PositiveInfinitySymbolで指定されたカルチャのNumberFormatInfo.NegativeInfinitySymbolNumberFormatInfo.NaNSymbol、またはproviderシンボルを含めることができます。 この文字列比較は、.NET Core 3.0 以降のバージョンでは大文字と小文字は区別されませんが、.NET Framework を含む以前のバージョンでは大文字と小文字が区別されます。 s パラメーターには、次の形式の文字列を含めることができます。

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

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

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

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

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

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

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

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

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

sSingleデータ型の範囲外の場合、メソッドは .NET Framework でOverflowExceptionをスローします。 .NET Core 3.0 以降のバージョンでは、Single.NegativeInfinitys未満の場合はSingle.MinValueを返し、Single.PositiveInfinitysより大きい場合はSingle.MaxValueします。

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

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

こちらもご覧ください

適用対象

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

ソース:
Single.cs
ソース:
Single.cs
ソース:
Single.cs
ソース:
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(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

ソース:
Single.cs
ソース:
Single.cs
ソース:
Single.cs
ソース:
Single.cs
ソース:
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に存在できるスタイル要素を示す列挙値のビットごとの組み合わせ。 指定する一般的な値は、Floatと組み合わせてAllowThousands

provider
IFormatProvider

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

返品

sで指定された数値または記号に相当する単精度浮動小数点数。

実装

例外

s は数値を表しません。

styleNumberStyles 値ではありません。

-又は-

styleAllowHexSpecifier 値です。

注釈

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

sSingleデータ型の範囲外の場合、Single.NegativeInfinitys未満の場合はSingle.MinValueを返し、Single.PositiveInfinitysより大きい場合はSingle.MaxValue

適用対象

Parse(String, NumberStyles, IFormatProvider)

ソース:
Single.cs
ソース:
Single.cs
ソース:
Single.cs
ソース:
Single.cs
ソース:
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に存在できるスタイル要素を示す列挙値のビットごとの組み合わせ。 指定する一般的な値は、Floatと組み合わせてAllowThousands

provider
IFormatProvider

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

返品

sで指定された数値または記号に相当する単精度浮動小数点数。

実装

例外

snullです。

s は数値を表しません。

styleNumberStyles 値ではありません。

-又は-

styleAllowHexSpecifier 値です。

.NET Framework のみ: s、Single.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 仕様で必要に応じて、大きすぎる値は PositiveInfinity または NegativeInfinity に丸められます。 以前のバージョン (.NET Framework を含む) では、大きすぎる値を解析するとエラーが発生しました。

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

s パラメーターには、NumberFormatInfo.PositiveInfinitySymbolで指定されたカルチャのNumberFormatInfo.NegativeInfinitySymbolNumberFormatInfo.NaNSymbol、またはproviderシンボルを含めることができます。 この文字列比較は、.NET Core 3.0 以降のバージョンでは大文字と小文字は区別されませんが、.NET Framework を含む以前のバージョンでは大文字と小文字が区別されます。 styleの値に応じて、s パラメーターは次の形式をとることもできます。

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

角かっこ ([ と ]) で囲まれた要素は省略可能です。 次の表では、各要素について説明します。

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

手記

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

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

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

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

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

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

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

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

sSingleデータ型の範囲外の場合、メソッドは .NET Framework でOverflowExceptionをスローします。 .NET Core 3.0 以降のバージョンでは、Single.NegativeInfinitys未満の場合はSingle.MinValueを返し、Single.PositiveInfinitysより大きい場合はSingle.MaxValueします。

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

こちらもご覧ください

適用対象