Muokkaa

Jaa


Decimal.ToString Method

Definition

Converts the numeric value of this instance to its equivalent String representation.

Overloads

ToString()

Converts the numeric value of this instance to its equivalent string representation.

ToString(IFormatProvider)

Converts the numeric value of this instance to its equivalent string representation using the specified culture-specific format information.

ToString(String)

Converts the numeric value of this instance to its equivalent string representation, using the specified format.

ToString(String, IFormatProvider)

Converts the numeric value of this instance to its equivalent string representation using the specified format and culture-specific format information.

ToString()

Source:
Decimal.cs
Source:
Decimal.cs
Source:
Decimal.cs

Converts the numeric value of this instance to its equivalent string representation.

public:
 override System::String ^ ToString();
public override string ToString ();
override this.ToString : unit -> string
Public Overrides Function ToString () As String

Returns

A string that represents the value of this instance.

Examples

The following example displays a Decimal value using the default ToString() method. It also displays the string representations of the Decimal value that result from using a number of standard format specifiers.

decimal value = -16325.62m;
// Display value using default ToString method.
Console.WriteLine(value.ToString());            // Displays -16325.62
// Display value using some standard format specifiers.
Console.WriteLine(value.ToString("G"));         // Displays -16325.62
Console.WriteLine(value.ToString("C"));         // Displays ($16,325.62)
Console.WriteLine(value.ToString("F"));         // Displays -16325.62
let value = -16325.62m
// Display value using default ToString method.
printfn $"{value.ToString()}"            // Displays -16325.62
// Display value using some standard format specifiers.
printfn $"""{value.ToString "G"}"""      // Displays -16325.62
printfn $"""{value.ToString "C"}"""      // Displays ($16,325.62)
printfn $"""{value.ToString "F"}"""      // Displays -16325.62
Dim value As Decimal = -16325.62d
' Display value using default ToString method.
Console.WriteLine(value.ToString())            ' Displays -16325.62
' Display value using some standard format specifiers.
Console.WriteLine(value.ToString("G"))         ' Displays -16325.62
Console.WriteLine(value.ToString("C"))         ' Displays ($16,325.62)
Console.WriteLine(value.ToString("F"))         ' Displays -16325.62

The following example displays the amount of money in an account.

   public ref class PiggyBank
   {
   public:
      void AddPenny()
      {
         MyFortune = Decimal::Add( MyFortune, (Decimal).01 );
      }

      virtual System::String^ ToString() override
      {
         return MyFortune.ToString("C")+" in piggy bank";
      }

   protected:
      Decimal MyFortune;
   };
}
class PiggyBank {
    public void AddPenny() {
        MyFortune = Decimal.Add(MyFortune, .01m);
    }

    public override string ToString() {
        return MyFortune.ToString("C")+" in piggy bank";
    }

    protected decimal MyFortune;
}
type PiggyBank() =
    let mutable myFortune = 0m

    member _.AddPenny() =
        myFortune <- Decimal.Add(myFortune, 0.01m)

    override _.ToString() =
        $"{myFortune:C} in piggy bank"
Class PiggyBank

    Public Sub AddPenny()
        MyFortune = [Decimal].Add(MyFortune, 0.01D)
    End Sub

    Public Overrides Function ToString() As String
        Return MyFortune.ToString("C") + " in piggy bank"
    End Function

    Protected MyFortune As Decimal
End Class

Remarks

The ToString() method formats a Decimal value in the default ("G", or general) format of the current culture. If you want to specify a different format or culture, use the other overloads of the ToString method, as follows:

To use format For culture Use the overload
Default ("G") format A specific culture ToString(IFormatProvider)
A specific format Default (current) culture ToString(String)
A specific format A specific culture ToString(String, IFormatProvider)

.NET provides extensive formatting support, which is described in greater detail in the following formatting topics:

See also

Applies to

ToString(IFormatProvider)

Source:
Decimal.cs
Source:
Decimal.cs
Source:
Decimal.cs

Converts the numeric value of this instance to its equivalent string representation using the specified culture-specific format information.

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

Parameters

provider
IFormatProvider

An object that supplies culture-specific formatting information.

Returns

The string representation of the value of this instance as specified by provider.

Implements

Examples

The following example displays the string representation of a Decimal value using CultureInfo objects that represent several different cultures.

decimal value = -16325.62m;
// 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.62
//       -16325.62
//       -16325,62
let value = -16325.62m
// 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.62
//       -16325.62
//       -16325,62
Dim value As Decimal = -16325.62d
' 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.62
'       -16325.62
'       -16325,62

Remarks

The ToString(IFormatProvider) method formats a Decimal value in the default ("G", or general) format of a specified culture. If you want to specify a different format or the current culture, use the other overloads of the ToString method, as follows:

To use format For culture Use the overload
Default ("G") format Default (current) culture ToString()
A specific format Default (current) culture ToString(String)
A specific format A specific culture ToString(String, IFormatProvider)

.NET provides extensive formatting support, which is described in greater detail in the following formatting topics:

The provider parameter is an IFormatProvider implementation whose IFormatProvider.GetFormat method returns a NumberFormatInfo object. Typically, provider is a NumberFormatInfo object or a CultureInfo object. The provider parameter supplies culture-specific information used in formatting. If provider is null, the thread current culture is used.

To convert a Decimal value to its string representation using a specified culture and a specific format string, call the Decimal.ToString(String, IFormatProvider) method.

See also

Applies to

ToString(String)

Source:
Decimal.cs
Source:
Decimal.cs
Source:
Decimal.cs

Converts the numeric value of this instance to its equivalent string representation, using the specified format.

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

Parameters

format
String

A standard or custom numeric format string.

Returns

The string representation of the value of this instance as specified by format.

Exceptions

format is invalid.

Examples

The following example displays a Decimal value using each of the supported standard numeric format specifiers, together with two custom numeric format strings. In converting the numeric values to strings, the example uses the formatting conventions of the en-US culture.

decimal value = 16325.62m;
string specifier;

// Use standard numeric format specifiers.
specifier = "G";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    G: 16325.62
specifier = "C";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    C: $16,325.62
specifier = "E04";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    E04: 1.6326E+004
specifier = "F";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    F: 16325.62
specifier = "N";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    N: 16,325.62
specifier = "P";
Console.WriteLine("{0}: {1}", specifier, (value/10000).ToString(specifier));
// Displays:    P: 163.26 %

// Use custom numeric format specifiers.
specifier = "0,0.000";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays:    0,0.000: 16,325.620
specifier = "#,#.00#;(#,#.00#)";
Console.WriteLine("{0}: {1}", specifier, (value*-1).ToString(specifier));
// Displays:    #,#.00#;(#,#.00#): (16,325.62)
let value = 16325.62m

// Use standard numeric format specifiers.
let specifier = "G"
printfn $"{specifier}: {value.ToString specifier}"
// Displays:    G: 16325.62
let specifier = "C"
printfn $"{specifier}: {value.ToString specifier}"
// Displays:    C: $16,325.62
let specifier = "E04"
printfn $"{specifier}: {value.ToString specifier}"
// Displays:    E04: 1.6326E+004
let specifier = "F"
printfn $"{specifier}: {value.ToString specifier}"
// Displays:    F: 16325.62
let specifier = "N"
printfn $"{specifier}: {value.ToString specifier}"
// Displays:    N: 16,325.62
let specifier = "P"
printfn $"{specifier}: {(value / 10000m).ToString specifier}"
// Displays:    P: 163.26 %

// Use custom numeric format specifiers.
let specifier = "0,0.000"
printfn $"{specifier}: {value.ToString specifier}"
// Displays:    0,0.000: 16,325.620
let specifier = "#,#.00#(#,#.00#)"
printfn $"{specifier}: {(value * -1m).ToString specifier}"
// Displays:    #,#.00#(#,#.00#): (16,325.62)
Dim value As Decimal = 16325.62d
Dim specifier As String

' Use standard numeric format specifiers.
specifier = "G"
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier))
' Displays:    G: 16325.62
specifier = "C"
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier))
' Displays:    C: $16,325.62
specifier = "E04"
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier))
' Displays:    E04: 1.6326E+004
specifier = "F"
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier))
' Displays:    F: 16325.62
specifier = "N"
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier))
' Displays:    N: 16,325.62
specifier = "P"
Console.WriteLine("{0}: {1}", specifier, (value/10000).ToString(specifier))
' Displays:    P: 163.26 %

' Use custom numeric format specifiers.
specifier = "0,0.000"
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier))
' Displays:    0,0.000: 16,325.620
specifier = "#,#.00#;(#,#.00#)"
Console.WriteLine("{0}: {1}", specifier, (value*-1).ToString(specifier))
' Displays:    #,#.00#;(#,#.00#): (16,325.62)

Remarks

The ToString(String) method formats a Decimal value in a specified format by using the conventions of the current culture. If you want to use the default ("G", or general) format or specify a different culture, use the other overloads of the ToString method, as follows:

To use format For culture Use the overload
Default ("G") format Default (current) culture ToString()
Default ("G") format A specific culture ToString(IFormatProvider)
A specific format A specific culture ToString(String, IFormatProvider)

The ToString method uses the standard or custom numeric format string specified by the format parameter to convert the value of the current instance into its string representation. The format parameter can be any valid standard numeric format specifier except for D, R, and X, as well as any combination of custom numeric format specifiers. If format is null or an empty string, the return value of this instance is formatted with the general numeric format specifier (G).

.NET provides extensive formatting support, which is described in greater detail in the following formatting topics:

The return value is formatted by using the NumberFormatInfo object for the current culture. To apply the formatting conventions of a specified culture, call the ToString(String, IFormatProvider) method.

See also

Applies to

ToString(String, IFormatProvider)

Source:
Decimal.cs
Source:
Decimal.cs
Source:
Decimal.cs

Converts the numeric value of this instance to its equivalent string representation using the specified format and culture-specific format information.

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

Parameters

format
String

A numeric format string.

provider
IFormatProvider

An object that supplies culture-specific formatting information.

Returns

The string representation of the value of this instance as specified by format and provider.

Implements

Exceptions

format is invalid.

Examples

The following example displays a Decimal value using each of the supported standard numeric format specifiers for several different cultures.

decimal value = 16325.62m;
string specifier;
CultureInfo culture;

// Use standard numeric format specifiers.
specifier = "G";
culture = CultureInfo.CreateSpecificCulture("eu-ES");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    16325,62
Console.WriteLine(value.ToString(specifier, CultureInfo.InvariantCulture));
// Displays:    16325.62

specifier = "C";
culture = CultureInfo.CreateSpecificCulture("en-US");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    $16,325.62
culture = CultureInfo.CreateSpecificCulture("en-GB");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    £16,325.62

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,62
culture = CultureInfo.CreateSpecificCulture("en-CA");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    16325.62

specifier = "N";
culture = CultureInfo.CreateSpecificCulture("es-ES");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    16.325,62
culture = CultureInfo.CreateSpecificCulture("fr-CA");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    16 325,62

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.62m

// Use standard numeric format specifiers.
let specifier = "G"
let culture = CultureInfo.CreateSpecificCulture "eu-ES"
Console.WriteLine(value.ToString(specifier, culture))
// Displays:    16325,62
Console.WriteLine(value.ToString(specifier, CultureInfo.InvariantCulture))
// Displays:    16325.62

let specifier = "C"
let culture = CultureInfo.CreateSpecificCulture "en-US"
Console.WriteLine(value.ToString(specifier, culture))
// Displays:    $16,325.62
let culture = CultureInfo.CreateSpecificCulture "en-GB"
Console.WriteLine(value.ToString(specifier, culture))
// Displays:    £16,325.62

let specifier = "E04"
let culture = CultureInfo.CreateSpecificCulture "sv-SE"
Console.WriteLine(value.ToString(specifier, culture))
// Displays: 1,6326E+004
let culture = CultureInfo.CreateSpecificCulture "en-NZ"
Console.WriteLine(value.ToString(specifier, culture))
// Displays:    1.6326E+004

let specifier = "F"
let culture = CultureInfo.CreateSpecificCulture "fr-FR"
Console.WriteLine(value.ToString(specifier, culture))
// Displays:    16325,62
let culture = CultureInfo.CreateSpecificCulture "en-CA"
Console.WriteLine(value.ToString(specifier, culture))
// Displays:    16325.62

let specifier = "N"
let culture = CultureInfo.CreateSpecificCulture "es-ES"
Console.WriteLine(value.ToString(specifier, culture))
// Displays:    16.325,62
let culture = CultureInfo.CreateSpecificCulture "fr-CA"
Console.WriteLine(value.ToString(specifier, culture))
// Displays:    16 325,62

let specifier = "P"
let culture = CultureInfo.InvariantCulture
printfn $"{(value / 10000m).ToString(specifier, culture)}"
// Displays:    163.26 %
let culture = CultureInfo.CreateSpecificCulture "ar-EG"
printfn $"{(value / 10000m).ToString(specifier, culture)}"
// Displays:    163.256 %
Dim value As Decimal = 16325.62d
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,62
Console.WriteLine(value.ToString(specifier, CultureInfo.InvariantCulture))
' Displays:    16325.62

specifier = "C"
culture = CultureInfo.CreateSpecificCulture("en-US")
Console.WriteLine(value.ToString(specifier, culture))
' Displays:    $16,325.62
culture = CultureInfo.CreateSpecificCulture("en-GB")
Console.WriteLine(value.ToString(specifier, culture))
' Displays:    £16,325.62

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,62
culture = CultureInfo.CreateSpecificCulture("en-CA")
Console.WriteLine(value.ToString(specifier, culture))
' Displays:    16325.62

specifier = "N"
culture = CultureInfo.CreateSpecificCulture("es-ES")
Console.WriteLine(value.ToString(specifier, culture))
' Displays:    16.325,62
culture = CultureInfo.CreateSpecificCulture("fr-CA")
Console.WriteLine(value.ToString(specifier, culture))
' Displays:    16 325,62

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 %

Remarks

The ToString(String, IFormatProvider) method formats a Decimal value in a specified format of a specified culture. If you want to use default format or culture settings, use the other overloads of the ToString method, as follows:

To use format For culture Use the overload
Default ("G") format Default (current) culture ToString()
Default ("G") format A specific culture ToString(IFormatProvider)
A specific format Default (current) culture ToString(String)

The format parameter can be any valid standard numeric format specifier except for D, R, and X, as well as any combination of custom numeric format specifiers. If format is null or an empty string, the return value for this instance is formatted with the general numeric format specifier ("G").

.NET provides extensive formatting support, which is described in greater detail in the following formatting topics:

The provider parameter is an IFormatProvider implementation whose IFormatProvider.GetFormat method returns a NumberFormatInfo object. Typically, provider is a NumberFormatInfo or CultureInfo object. The provider parameter supplies culture-specific information used in formatting. If provider is null, the thread current culture is used.

See also

Applies to