Single.ToString 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將這個實例的數值轉換為其相等的字串表示。
多載
| 名稱 | Description |
|---|---|
| ToString() |
將這個實例的數值轉換為其相等的字串表示。 |
| ToString(IFormatProvider) |
利用指定的文化特定格式資訊,將此實例的數值轉換為其等效字串表示。 |
| ToString(String) |
使用指定的格式,將這個實例的數值轉換為其相等的字串表示。 |
| ToString(String, IFormatProvider) |
使用指定的格式和文化特性特定格式資訊,將這個實例的數值轉換為其相等的字串表示。 |
ToString()
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
將這個實例的數值轉換為其相等的字串表示。
public:
override System::String ^ ToString();
public override string ToString();
override this.ToString : unit -> string
Public Overrides Function ToString () As String
傳回
此實例值的字串表示。
範例
以下範例使用預設 Single.ToString 方法來顯示多個 Single 值的字串表示。
float number;
number = 1.6E20F;
// Displays 1.6E+20.
Console.WriteLine(number.ToString());
number = 1.6E2F;
// Displays 160.
Console.WriteLine(number.ToString());
number = -3.541F;
// Displays -3.541.
Console.WriteLine(number.ToString());
number = -1502345222199E-07F;
// Displays -150234.5222199.
Console.WriteLine(number.ToString());
number = -15023452221990199574E-09F;
// Displays -15023452221.9902.
Console.WriteLine(number.ToString());
number = .60344F;
// Displays 0.60344.
Console.WriteLine(number.ToString());
number = .000000001F;
// Displays 1E-09.
Console.WriteLine(number.ToString());
let number = 1.6E20F
// Displays 1.6E+20.
printfn $"{number.ToString()}"
let number = 1.6E2F
// Displays 160.
printfn $"{number.ToString()}"
let number = -3.541F
// Displays -3.541.
printfn $"{number.ToString()}"
let number = -1502345222199E-07F
// Displays -150234.5222199.
printfn $"{number.ToString()}"
let number = -15023452221990199574E-09F
// Displays -15023452221.9902.
printfn $"{number.ToString()}"
let number = 0.60344F
// Displays 0.60344.
printfn $"{number.ToString()}"
let number = 0.000000001F
// Displays 1E-09.
printfn $"{number.ToString()}"
Dim number As Single
number = 1.6E20
' Displays 1.6E+20.
Console.WriteLine(number.ToString())
number = 1.6E2
' Displays 160.
Console.WriteLine(number.ToString())
number = -3.541
' Displays -3.541.
Console.WriteLine(number.ToString())
number = -1502345222199E-07
' Displays -150234.5222199.
Console.WriteLine(number.ToString())
number = -15023452221990199574E-09
' Displays -15023452221.9902.
Console.WriteLine(number.ToString())
number = .60344
' Displays 0.60344.
Console.WriteLine(number.ToString())
number = .000000001
' Displays 1E-09.
Console.WriteLine(number.ToString())
以下程式碼範例說明了該方法與該Parse(String)方法的使用ToString()。
bool done = false;
string inp;
do
{
Console.Write("Enter a real number: ");
inp = Console.ReadLine();
try
{
s = Single.Parse(inp);
Console.WriteLine("You entered {0}.", s.ToString());
done = true;
}
catch (FormatException)
{
Console.WriteLine("You did not enter a number.");
}
catch (Exception e)
{
Console.WriteLine("An exception occurred while parsing your response: {0}", e.ToString());
}
} while (!done);
let mutable finished = false
while not finished do
printf "Enter a real number: "
let inp = stdin.ReadLine()
try
let s = Single.Parse inp
printfn $"You entered {s}."
finished <- true
with
| :? FormatException ->
printfn "You did not enter a number."
| e ->
printfn "An exception occurred while parsing your response: {e}"
Dim Done As Boolean = False
Dim Inp As String
Do
Console.Write("Enter a real number: ")
Inp = Console.ReadLine()
Try
S = Single.Parse(Inp)
Console.WriteLine("You entered " + S.ToString() + ".")
Done = True
Catch E As FormatException
Console.WriteLine("You did not enter a number.")
Catch E As Exception
Console.WriteLine("An exception occurred while parsing your response: " + E.ToString())
End Try
Loop While Not Done
備註
該 ToString() 方法以當前文化的預設(「G」或一般)格式格式格式化值 Single 。 如果你想指定不同的格式或文化,請使用方法的其他超載 ToString ,如下:
| 使用格式 | 為了文化 | 利用過載 |
|---|---|---|
| 預設(「G」)格式 | 特定文化 | ToString(IFormatProvider) |
| 特定格式 | 預設(現行)文化 | ToString(String) |
| 特定格式 | 特定文化 | ToString(String, IFormatProvider) |
回傳值可以是 PositiveInfinitySymbol、 NegativeInfinitySymbol、 NaNSymbol,或是形式如 的字串:
[符號]整數數字[.[小數數字]][e[符號]指數數字]
選擇性專案會以方括弧 ([ 和 ]) 框住。 包含「digits」 一詞的元素包含一系列從 0 到 9 的數位字元。 下表列出每個元素:
| 元素 | Description |
|---|---|
| 簽署 | 負號或正負號符號。 |
| 整數數字 | 一連串數字表示該數字的整數部分。 如果有小數位數,整數位數就可能不存在。 |
| '.' | 特定文化特性的小數點符號。 |
| 小數數字 | 一連串數字表示數字的分數部分。 |
| 『e』 | 小寫字母 'e',表示指數(科學)符號。 |
| 指數位數 | 一連串數字指定指數。 |
回傳值的例子包括「100」、「-123,456,789」、「123.45e+6」、「500」、「3.1416」、「600」、「-0.123」和「-Infinity」。
.NET 提供廣泛的格式支援,如下列格式化主題中更詳細地說明:
另請參閱
適用於
ToString(IFormatProvider)
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
利用指定的文化特定格式資訊,將此實例的數值轉換為其等效字串表示。
public:
virtual System::String ^ ToString(IFormatProvider ^ provider);
public:
System::String ^ ToString(IFormatProvider ^ provider);
public string ToString(IFormatProvider provider);
public string ToString(IFormatProvider? provider);
override this.ToString : IFormatProvider -> string
Public Function ToString (provider As IFormatProvider) As String
參數
- provider
- IFormatProvider
物件,提供特定文化特性的格式資訊。
傳回
由 所指定的 provider這個實例值的字串表示。
實作
範例
以下範例展示了使用Single代表多種不同文化的物件,將兩個CultureInfo值的字串表示方式呈現。
float value;
value = -16325.62015F;
// Display value using the invariant culture.
Console.WriteLine(value.ToString(CultureInfo.InvariantCulture));
// Display value using the en-GB culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("en-GB")));
// Display value using the de-DE culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("de-DE")));
value = 16034.125E21F;
// Display value using the invariant culture.
Console.WriteLine(value.ToString(CultureInfo.InvariantCulture));
// Display value using the en-GB culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("en-GB")));
// Display value using the de-DE culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("de-DE")));
// This example displays the following output to the console:
// -16325.62015
// -16325.62015
// -16325,62015
// 1.6034125E+25
// 1.6034125E+25
// 1,6034125E+25
let value = -16325.62015F
// Display value using the invariant culture.
printfn $"{value.ToString CultureInfo.InvariantCulture}"
// Display value using the en-GB culture.
printfn $"""{value.ToString(CultureInfo.CreateSpecificCulture "en-GB")}"""
// Display value using the de-DE culture.
printfn $"""{value.ToString(CultureInfo.CreateSpecificCulture "de-DE")}"""
let value = 16034.125E21F
// Display value using the invariant culture.
printfn $"{value.ToString CultureInfo.InvariantCulture}"
// Display value using the en-GB culture.
printfn $"""{value.ToString(CultureInfo.CreateSpecificCulture "en-GB")}"""
// Display value using the de-DE culture.
printfn $"""{value.ToString(CultureInfo.CreateSpecificCulture "de-DE")}"""
// This example displays the following output to the console:
// -16325.62015
// -16325.62015
// -16325,62015
// 1.6034125E+25
// 1.6034125E+25
// 1,6034125E+25
Dim value As Single
value = -16325.62015
' Display value using the invariant culture.
Console.WriteLine(value.ToString(CultureInfo.InvariantCulture))
' Display value using the en-GB culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("en-GB")))
' Display value using the de-DE culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("de-DE")))
value = 16034.125E21
' Display value using the invariant culture.
Console.WriteLine(value.ToString(CultureInfo.InvariantCulture))
' Display value using the en-GB culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("en-GB")))
' Display value using the de-DE culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("de-DE")))
' This example displays the following output to the console:
' -16325.62015
' -16325.62015
' -16325,62015
' 1.6034125E+25
' 1.6034125E+25
' 1,6034125E+25
備註
該 ToString(IFormatProvider) 方法會將指定文化的預設(「G」或一般)格式格式化 Single 一個值。 如果你想指定不同的格式或目前的文化,請使用方法的其他超載 ToString ,如下:
| 使用格式 | 為了文化 | 利用過載 |
|---|---|---|
| 預設(「G」)格式 | 預設(現行)文化 | ToString() |
| 特定格式 | 預設(現行)文化 | ToString(String) |
| 特定格式 | 特定文化 | ToString(String, IFormatProvider) |
回傳值可以是 PositiveInfinitySymbol、 NegativeInfinitySymbol、 NaNSymbol,或是形式如 的字串:
[符號]整數數字[.[小數數字]][e[符號]指數數字]
選擇性專案會以方括弧 ([ 和 ]) 框住。 包含「數字」一詞的元素由一連串從0到9的數字字元組成。 下表列出每個元素。
| 元素 | Description |
|---|---|
| sign | 負號或正負號符號。 |
| 整數數字 | 一連串數字表示該數字的整數部分。 如果有小數位數,整數位數就可能不存在。 |
| '.' | 特定文化特性的小數點符號。 |
| 小數數字 | 一連串數字表示數字的分數部分。 |
| 『e』 | 小寫字母 'e',表示指數(科學)符號。 |
| 指數位數 | 一連串數字指定指數。 |
回傳值的例子包括「100」、「-123,456,789」、「123.45e+6」、「500」、「3.1416」、「600」、「-0.123」和「-Infinity」。
.NET 提供廣泛的格式支援,如下列格式化主題中更詳細地說明:
參數 provider 是一個 IFormatProvider 實作,其 GetFormat 方法回傳一個 NumberFormatInfo 物件。 通常, provider 是一個 CultureInfo 物件或一個 NumberFormatInfo 物件。 該 provider 參數提供用於格式化的文化特定資訊。 若 provider 為 , null則回傳值會使用 NumberFormatInfo 當前文化的資料來格式化。
若要將值轉換為 Single 其字串表示,使用指定的文化與特定格式字串,請呼叫該 Single.ToString(String, IFormatProvider) 方法。
另請參閱
適用於
ToString(String)
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
使用指定的格式,將這個實例的數值轉換為其相等的字串表示。
public:
System::String ^ ToString(System::String ^ format);
public string ToString(string format);
public string ToString(string? format);
override this.ToString : string -> string
Public Function ToString (format As String) As String
參數
- format
- String
一個數字格式的字串。
傳回
由 所指定的 format這個實例值的字串表示。
例外狀況
format 無效。
範例
以下範例定義一個數值,並透過「C」標準數字格式字串將其格式化為貨幣值,並以「N」標準數字格式字串作為小數點後三位的數值。 結果字串的格式化依據 en-US 文化的慣例。 欲了解更多數值格式字串的資訊,請參閱 標準數值格式字串 與 自訂數值格式字串。
using System;
public class Example
{
public static void Main()
{
Double number = 1764.3789;
// Format as a currency value.
Console.WriteLine(number.ToString("C"));
// Format as a numeric value with 3 decimal places.
Console.WriteLine(number.ToString("N3"));
}
}
// The example displays the following output:
// $1,764.38
// 1,764.379
let number = 1764.3789
// Format as a currency value.
printfn $"""{number.ToString "C"}"""
// Format as a numeric value with 3 decimal places.
printfn $"""{number.ToString "N3"}"""
// The example displays the following output:
// $1,764.38
// 1,764.379
Module Example
Public Sub Main()
Dim number As Single = 1764.3789
' Format as a currency value.
Console.WriteLine(number.ToString("C"))
' Format as a numeric value with 3 decimal places.
Console.WriteLine(number.ToString("N3"))
End Sub
End Module
' The example displays the following output:
' $1,764.38
' 1,764.379
以下範例展示了多個 Single 使用支援標準數值格式指定符的數值,並搭配兩個自訂數值格式字串。 其中一串自訂格式字串示範如何用前置零填充 Single 值。 在將數值轉換為字串時,範例採用了 en-US 文化的格式慣例。
float[] numbers= { 1054.32179F, -195489100.8377F, 1.0437E21F,
-1.0573e-05F };
string[] specifiers = { "C", "E", "e", "F", "G", "N", "P",
"R", "#,000.000", "0.###E-000",
"000,000,000,000.00###" };
foreach (float number in numbers)
{
Console.WriteLine("Formatting of {0}:", number);
foreach (string specifier in specifiers)
Console.WriteLine(" {0,5}: {1}",
specifier, number.ToString(specifier));
Console.WriteLine();
}
// The example displays the following output to the console:
// Formatting of 1054.32179:
// C: $1,054.32
// E: 1.054322E+003
// e: 1.054322e+003
// F: 1054.32
// G: 1054.32179
// N: 1,054.32
// P: 105,432.18 %
// R: 1054.32179
// #,000.000: 1,054.322
// 0.###E-000: 1.054E003
// 000,000,000,000.00###: 000,000,001,054.322
//
// Formatting of -195489100.8377:
// C: ($195,489,100.84)
// E: -1.954891E+008
// e: -1.954891e+008
// F: -195489100.84
// G: -195489100.8377
// N: -195,489,100.84
// P: -19,548,910,083.77 %
// R: -195489100.8377
// #,000.000: -195,489,100.838
// 0.###E-000: -1.955E008
// 000,000,000,000.00###: -000,195,489,100.00
//
// Formatting of 1.0437E+21:
// C: $1,043,700,000,000,000,000,000.00
// E: 1.043700E+021
// e: 1.043700e+021
// F: 1043700000000000000000.00
// G: 1.0437E+21
// N: 1,043,700,000,000,000,000,000.00
// P: 104,370,000,000,000,000,000,000.00 %
// R: 1.0437E+21
// #,000.000: 1,043,700,000,000,000,000,000.000
// 0.###E-000: 1.044E021
// 000,000,000,000.00###: 1,043,700,000,000,000,000,000.00
//
// Formatting of -1.0573E-05:
// C: $0.00
// E: -1.057300E-005
// e: -1.057300e-005
// F: 0.00
// G: -1.0573E-05
// N: 0.00
// P: 0.00 %
// R: -1.0573E-05
// #,000.000: 000.000
// 0.###E-000: -1.057E-005
// 000,000,000,000.00###: -000,000,000,000.00001
let numbers =
[| 1054.32179F; -195489100.8377F; 1.0437E21F; -1.0573e-05F |]
let specifiers =
[| "C"; "E"; "e"; "F"; "G"; "N"; "P"
"R"; "#,000.000"; "0.###E-000"
"000,000,000,000.00###" |]
for number in numbers do
printfn $"Formatting of {number}:"
for specifier in specifiers do
printfn $" {specifier,5}: {number.ToString specifier}"
printfn ""
// The example displays the following output to the console:
// Formatting of 1054.32179:
// C: $1,054.32
// E: 1.054322E+003
// e: 1.054322e+003
// F: 1054.32
// G: 1054.32179
// N: 1,054.32
// P: 105,432.18 %
// R: 1054.32179
// #,000.000: 1,054.322
// 0.###E-000: 1.054E003
// 000,000,000,000.00###: 000,000,001,054.322
//
// Formatting of -195489100.8377:
// C: ($195,489,100.84)
// E: -1.954891E+008
// e: -1.954891e+008
// F: -195489100.84
// G: -195489100.8377
// N: -195,489,100.84
// P: -19,548,910,083.77 %
// R: -195489100.8377
// #,000.000: -195,489,100.838
// 0.###E-000: -1.955E008
// 000,000,000,000.00###: -000,195,489,100.00
//
// Formatting of 1.0437E+21:
// C: $1,043,700,000,000,000,000,000.00
// E: 1.043700E+021
// e: 1.043700e+021
// F: 1043700000000000000000.00
// G: 1.0437E+21
// N: 1,043,700,000,000,000,000,000.00
// P: 104,370,000,000,000,000,000,000.00 %
// R: 1.0437E+21
// #,000.000: 1,043,700,000,000,000,000,000.000
// 0.###E-000: 1.044E021
// 000,000,000,000.00###: 1,043,700,000,000,000,000,000.00
//
// Formatting of -1.0573E-05:
// C: $0.00
// E: -1.057300E-005
// e: -1.057300e-005
// F: 0.00
// G: -1.0573E-05
// N: 0.00
// P: 0.00 %
// R: -1.0573E-05
// #,000.000: 000.000
// 0.###E-000: -1.057E-005
// 000,000,000,000.00###: -000,000,000,000.00001
Dim numbers() As Single = {1054.32179, -195489100.8377, 1.0437E21, _
-1.0573e-05}
Dim specifiers() As String = { "C", "E", "e", "F", "G", "N", "P", _
"R", "#,000.000", "0.###E-000", _
"000,000,000,000.00###"}
For Each number As Single In numbers
Console.WriteLine("Formatting of {0}:", number)
For Each specifier As String In specifiers
Console.WriteLine(" {0,5}: {1}", _
specifier, number.ToString(specifier))
Next
Console.WriteLine()
Next
' The example displays the following output to the console:
' Formatting of 1054.32179:
' C: $1,054.32
' E: 1.054322E+003
' e: 1.054322e+003
' F: 1054.32
' G: 1054.32179
' N: 1,054.32
' P: 105,432.18 %
' R: 1054.32179
' #,000.000: 1,054.322
' 0.###E-000: 1.054E003
' 000,000,000,000.00###: 000,000,001,054.322
'
' Formatting of -195489100.8377:
' C: ($195,489,100.84)
' E: -1.954891E+008
' e: -1.954891e+008
' F: -195489100.84
' G: -195489100.8377
' N: -195,489,100.84
' P: -19,548,910,083.77 %
' R: -195489100.8377
' #,000.000: -195,489,100.838
' 0.###E-000: -1.955E008
' 000,000,000,000.00###: -000,195,489,100.00
'
' Formatting of 1.0437E+21:
' C: $1,043,700,000,000,000,000,000.00
' E: 1.043700E+021
' e: 1.043700e+021
' F: 1043700000000000000000.00
' G: 1.0437E+21
' N: 1,043,700,000,000,000,000,000.00
' P: 104,370,000,000,000,000,000,000.00 %
' R: 1.0437E+21
' #,000.000: 1,043,700,000,000,000,000,000.000
' 0.###E-000: 1.044E021
' 000,000,000,000.00###: 1,043,700,000,000,000,000,000.00
'
' Formatting of -1.0573E-05:
' C: $0.00
' E: -1.057300E-005
' e: -1.057300e-005
' F: 0.00
' G: -1.0573E-05
' N: 0.00
' P: 0.00 %
' R: -1.0573E-05
' #,000.000: 000.000
' 0.###E-000: -1.057E-005
' 000,000,000,000.00###: -000,000,000,000.00001
備註
該 ToString(String) 方法透過使用當前文化的慣例,將 Single 值格式化成指定格式。 如果你想使用預設的(「G」或一般)格式或指定不同的文化,請使用方法的其他 ToString 超載,如下:
| 使用格式 | 為了文化 | 利用過載 |
|---|---|---|
| 預設(「G」)格式 | 預設(現行)文化 | ToString() |
| 預設(「G」)格式 | 特定文化 | ToString(IFormatProvider) |
| 特定格式 | 特定文化 | ToString(String, IFormatProvider) |
回傳值可以是 PositiveInfinitySymbol、 NegativeInfinitySymbol、 NaNSymbol,或是目前實例值的字串表示,依據 format。
該 format 參數可以是任何有效的標準數值格式指定符,除了 D 和 X 之外,也可以是任意自訂數值格式的組合。 若格式為 null 空字串,則回傳值會以一般數值格式指定符(“G”)格式化。
.NET 提供廣泛的格式支援,如下列格式化主題中更詳細地說明:
預設情況下,回傳值僅包含 7 位數的精確度,但內部最多維持 9 位數。 若該實例值大於7位數,則 ToString(String) 返回 PositiveInfinitySymbol 或 NegativeInfinitySymbol 取代預期數字。 若需要更高精確度,則以「G9」格式規範指定 format ,該規格始終回傳9位數的精度,或「R」格式,若該數字能以該精度表示,則回傳7位數;若該數字只能以最大精度表示,則回傳9位數。
另請參閱
適用於
ToString(String, IFormatProvider)
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
- 來源:
- Single.cs
使用指定的格式和文化特性特定格式資訊,將這個實例的數值轉換為其相等的字串表示。
public:
virtual System::String ^ ToString(System::String ^ format, IFormatProvider ^ provider);
public string ToString(string format, IFormatProvider provider);
public string ToString(string? format, IFormatProvider? provider);
override this.ToString : string * IFormatProvider -> string
Public Function ToString (format As String, provider As IFormatProvider) As String
參數
- format
- String
一個數字格式的字串。
- provider
- IFormatProvider
物件,提供特定文化特性的格式資訊。
傳回
由 和 format指定provider該實例值的字串表示。
實作
範例
以下範例 Single 展示了使用多種不同文化所支援的標準數值格式指定符。
float value = 16325.62901F;
string specifier;
CultureInfo culture;
// Use standard numeric format specifiers.
specifier = "G";
culture = CultureInfo.CreateSpecificCulture("eu-ES");
Console.WriteLine(value.ToString(specifier, culture));
// Displays: 16325,62901
Console.WriteLine(value.ToString(specifier, CultureInfo.InvariantCulture));
// Displays: 16325.62901
specifier = "C";
culture = CultureInfo.CreateSpecificCulture("en-US");
Console.WriteLine(value.ToString(specifier, culture));
// Displays: $16,325.63
culture = CultureInfo.CreateSpecificCulture("en-GB");
Console.WriteLine(value.ToString(specifier, culture));
// Displays: £16,325.63
specifier = "E04";
culture = CultureInfo.CreateSpecificCulture("sv-SE");
Console.WriteLine(value.ToString(specifier, culture));
// Displays: 1,6326E+004
culture = CultureInfo.CreateSpecificCulture("en-NZ");
Console.WriteLine(value.ToString(specifier, culture));
// Displays: 1.6326E+004
specifier = "F";
culture = CultureInfo.CreateSpecificCulture("fr-FR");
Console.WriteLine(value.ToString(specifier, culture));
// Displays: 16325,63
culture = CultureInfo.CreateSpecificCulture("en-CA");
Console.WriteLine(value.ToString(specifier, culture));
// Displays: 16325.63
specifier = "N";
culture = CultureInfo.CreateSpecificCulture("es-ES");
Console.WriteLine(value.ToString(specifier, culture));
// Displays: 16.325,63
culture = CultureInfo.CreateSpecificCulture("fr-CA");
Console.WriteLine(value.ToString(specifier, culture));
// Displays: 16 325,63
specifier = "P";
culture = CultureInfo.InvariantCulture;
Console.WriteLine((value/10000).ToString(specifier, culture));
// Displays: 163.26 %
culture = CultureInfo.CreateSpecificCulture("ar-EG");
Console.WriteLine((value/10000).ToString(specifier, culture));
// Displays: 163.256 %
let value = 16325.62901F
// Use standard numeric format specifiers.
let specifier = "G"
let culture = CultureInfo.CreateSpecificCulture "eu-ES"
printfn $"{value.ToString(specifier, culture)}"
// Displays: 16325,62901
printfn $"{value.ToString(specifier, CultureInfo.InvariantCulture)}"
// Displays: 16325.62901
let specifier = "C"
let culture = CultureInfo.CreateSpecificCulture "en-US"
printfn $"{value.ToString(specifier, culture)}"
// Displays: $16,325.63
let culture = CultureInfo.CreateSpecificCulture "en-GB"
printfn $"{value.ToString(specifier, culture)}"
// Displays: £16,325.63
let specifier = "E04"
let culture = CultureInfo.CreateSpecificCulture "sv-SE"
printfn $"{value.ToString(specifier, culture)}"
// Displays: 1,6326E+004
let culture = CultureInfo.CreateSpecificCulture "en-NZ"
printfn $"{value.ToString(specifier, culture)}"
// Displays: 1.6326E+004
let specifier = "F"
let culture = CultureInfo.CreateSpecificCulture "fr-FR"
printfn $"{value.ToString(specifier, culture)}"
// Displays: 16325,63
let culture = CultureInfo.CreateSpecificCulture "en-CA"
printfn $"{value.ToString(specifier, culture)}"
// Displays: 16325.63
let specifier = "N"
let culture = CultureInfo.CreateSpecificCulture "es-ES"
printfn $"{value.ToString(specifier, culture)}"
// Displays: 16.325,63
let culture = CultureInfo.CreateSpecificCulture "fr-CA"
printfn $"{value.ToString(specifier, culture)}"
// Displays: 16 325,63
let specifier = "P"
let culture = CultureInfo.InvariantCulture
printfn $"{(value / 10000f).ToString(specifier, culture)}"
// Displays: 163.26 %
let culture = CultureInfo.CreateSpecificCulture "ar-EG"
printfn $"{(value / 10000f).ToString(specifier, culture)}"
// Displays: 163.256 %
Dim value As Single = 16325.62901
Dim specifier As String
Dim culture As CultureInfo
' Use standard numeric format specifiers.
specifier = "G"
culture = CultureInfo.CreateSpecificCulture("eu-ES")
Console.WriteLine(value.ToString(specifier, culture))
' Displays: 16325,62901
Console.WriteLine(value.ToString(specifier, CultureInfo.InvariantCulture))
' Displays: 16325.62901
specifier = "C"
culture = CultureInfo.CreateSpecificCulture("en-US")
Console.WriteLine(value.ToString(specifier, culture))
' Displays: $16,325.63
culture = CultureInfo.CreateSpecificCulture("en-GB")
Console.WriteLine(value.ToString(specifier, culture))
' Displays: £16,325.63
specifier = "E04"
culture = CultureInfo.CreateSpecificCulture("sv-SE")
Console.WriteLine(value.ToString(specifier, culture))
' Displays: 1,6326E+004
culture = CultureInfo.CreateSpecificCulture("en-NZ")
Console.WriteLine(value.ToString(specifier, culture))
' Displays: 1.6326E+004
specifier = "F"
culture = CultureInfo.CreateSpecificCulture("fr-FR")
Console.WriteLine(value.ToString(specifier, culture))
' Displays: 16325,63
culture = CultureInfo.CreateSpecificCulture("en-CA")
Console.WriteLine(value.ToString(specifier, culture))
' Displays: 16325.63
specifier = "N"
culture = CultureInfo.CreateSpecificCulture("es-ES")
Console.WriteLine(value.ToString(specifier, culture))
' Displays: 16.325,63
culture = CultureInfo.CreateSpecificCulture("fr-CA")
Console.WriteLine(value.ToString(specifier, culture))
' Displays: 16 325,63
specifier = "P"
culture = CultureInfo.InvariantCulture
Console.WriteLine((value/10000).ToString(specifier, culture))
' Displays: 163.26 %
culture = CultureInfo.CreateSpecificCulture("ar-EG")
Console.WriteLine((value/10000).ToString(specifier, culture))
' Displays: 163.256 %
備註
該 ToString(String, IFormatProvider) 方法以指定文化的格式格式化值 Single 。 如果你想使用預設格式或文化設定,請使用方法的其他超載 ToString ,如下:
| 使用格式 | 為了文化 | 利用過載 |
|---|---|---|
| 預設(「G」)格式 | 預設(現行)文化 | ToString() |
| 預設(「G」)格式 | 特定文化 | ToString(IFormatProvider) |
| 特定格式 | 預設(現行)文化 | ToString(String) |
回傳值可以是 PositiveInfinitySymbol、 NegativeInfinitySymbol、 NaNSymbol,或是目前實例值的字串表示,依據 format。
該 format 參數可以是任何有效的標準數值格式指定符,除了 D 和 X 之外,也可以是任意自訂數值格式的組合。 若 format 或 null 為空字串,則此實例的回傳值會以一般的數值格式化(「G」)格式化。
.NET 提供廣泛的格式支援,如下列格式化主題中更詳細地說明:
參數 provider 是一個 IFormatProvider 實作,其 GetFormat 方法回傳一個 NumberFormatInfo 物件。 通常, provider 是一個 CultureInfo 物件或一個 NumberFormatInfo 物件。 該 provider 參數提供用於格式化的文化特定資訊。 若 provider 為 , null則返回值以當前文化的物件格式化 NumberFormatInfo 。
預設情況下,回傳值僅包含 7 位數的精確度,但內部最多維持 9 位數。 若該實例值大於7位數,則 ToString 返回 PositiveInfinitySymbol 或 NegativeInfinitySymbol 取代預期數字。 若需要更高精確度,則以「G9」格式規範指定 format ,該規格始終回傳9位數的精度,或「R」格式,若該數字能以該精度表示,則回傳7位數;若該數字只能以最大精度表示,則回傳9位數。