首页 > 开发 > 综合 > 正文

C#小Tip:数字格式化显示

2024-07-21 02:26:36
字体:
来源:转载
供稿:网友

using system;
class program
{

    static void main()
    {
        decimal thedecnumber = 12345.678m; //the "m" creates a literal of type decimal from a double
        //using the tostring method
        //the number in the format string is the precision specifier
        console.writeline("no formatting: " + thedecnumber.tostring());
        console.writeline("currency formatting: " + thedecnumber.tostring("c"));
        console.writeline("exponential formatting: " + thedecnumber.tostring("e"));
        console.writeline("fixed-point formatting: " + thedecnumber.tostring("f2"));
        console.writeline("general formatting: " + thedecnumber.tostring("g"));
        console.writeline("number formatting to 2 decimal places: " + thedecnumber.tostring("n2"));
        console.writeline("number formatting to 3 decimal places: " + thedecnumber.tostring("n3"));
        console.writeline("number formatting to 4 decimal places: " + thedecnumber.tostring("n4"));
        console.writeline("percent formatting: " + thedecnumber.tostring("p0"));

        int theintnumber = 123456;
        console.writeline("hexidecimal formatting (for integers): {0} = {1}", theintnumber, theintnumber.tostring("x"));

        double thedblnumber = 1234567890;
        console.writeline("custom formatting: {0} to us telephone {1}", thedblnumber, thedblnumber.tostring("(###) ### - ####"));

        //keep console open if not run through command prompt
        console.write("/npress enter to continue");
        console.readline();
    }
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表