C#数据decimal保留两位小数

decimal的toString示例,保留两位小数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestDecimal
{
    class Program
    {
        static void Main(string[] args)
        {
            decimal a1 = 4.335M;
            string q1 = a1.ToString("F");
            Console.WriteLine(q1);

            decimal a2 = 4.332M;
            string q2 = a2.ToString("F");
            Console.WriteLine(q2);

            decimal a3 = 4.332M;
            string q3 = a3.ToString("#0.00");
            Console.WriteLine(q3);

            decimal a4 = 4.355M;
            string q4 = a4.ToString("#0.00");
            Console.WriteLine(q4);

            decimal a5 = 4.5555M;
            string q5 = a5.ToString("#0.00");
            Console.WriteLine(q5);

            decimal a6 = 400M;
            string q6 = a6.ToString("#0.00");
            Console.WriteLine(q6);

            decimal a7 = 400M;
            string q7 = a7.ToString("F");
            Console.WriteLine(q7);

            decimal a8 = 400.2M;
            string q8 = a8.ToString("F");
            Console.WriteLine(q8);

            decimal a9 = 4.335M;
            string q9 = a9.ToString();
            Console.WriteLine(q9);

            decimal a10 = 4.335M;
            string q10 = a10.ToString("G");
            Console.WriteLine(q10);



            Console.ReadKey();
           


        }
    }
}

输出效果

4.34
4.33
4.33
4.36
4.56
400.00
400.00
400.20
4.335
4.335
原文地址:https://www.cnblogs.com/Tpf386/p/15726231.html