当前位置: 首页>>代码示例>>C#>>正文


C# Decimal.ToString方法代码示例

本文整理汇总了C#中System.Decimal.ToString方法的典型用法代码示例。如果您正苦于以下问题:C# Decimal.ToString方法的具体用法?C# Decimal.ToString怎么用?C# Decimal.ToString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Decimal的用法示例。


在下文中一共展示了Decimal.ToString方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1:

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
开发者ID:.NET开发者,项目名称:System,代码行数:11,代码来源:Decimal.ToString

示例2:

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 %
开发者ID:.NET开发者,项目名称:System,代码行数:51,代码来源:Decimal.ToString

示例3:

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)
开发者ID:.NET开发者,项目名称:System,代码行数:30,代码来源:Decimal.ToString

示例4: Displays

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
开发者ID:.NET开发者,项目名称:System,代码行数:7,代码来源:Decimal.ToString

示例5: AddPenny

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

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

    protected decimal MyFortune;
}
开发者ID:.NET开发者,项目名称:System,代码行数:11,代码来源:Decimal.ToString


注:本文中的System.Decimal.ToString方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。