zoukankan      html  css  js  c++  java
  • C#带格式输出

    代码清单 :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication
    {
        class Program
        {
            static void Main(string[] args)
            {
                //定义两个变量.
                int i = 23, j = 45;
                //输出
                Console.WriteLine("{0} + {1} = {2}",i,j,i+j); //{}代表断位付,一个{}代表一个变量, 一般从0开始.  结果等于 23 + 45 = 58
                //输出
                Console.WriteLine("{0,6}\n+{1,5}\n------\n{2,6}",i,j,i+j); //{}里的第一个参数已经明白了.代表一个变量,第二个参数就是宽度. 比如{0,6}就说明这个变量的宽度为6不足六位前面用空格替代,还有\n是代表换行符
                                                                                               /*输出结果为:
                                                                                                *           23
                                                                                                *     +    45
                                                                                                *     --------
                                                                                                *           58
                                                                                                */
    
                //再来一段输出,此次给数字前加上一个货币符号.
                decimal a = 75.18m; //浮点数(货币类型).此类型在赋值的时候需在后面加上一个m;
                Console.WriteLine("{0,6:C2}",a);//{}内的参数依次为: 0=变量, 6=宽度,C=货币符号,系统会根据所使用的电脑的语言自动修改符号,2=小数点保留位数,添入多少就保留到多少位,但是不能超过decimal的最高位.
            }
        }
    }
           
    
    // 最后总结一下, 其实格式化输出的控制符还有很多,这只是沧海一粟,以后还得多学习. 
  • 相关阅读:
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
  • 原文地址:https://www.cnblogs.com/mdnx/p/2655171.html
Copyright © 2011-2022 走看看