Làm thế nào để làm tròn một số thành số có hai chữ số thập phân trong CSharp? | How do to round a number to two decimal places in CSharp?

Bài viết này tôi sẽ hướng dẫn làm tròn một số thập phân thành một số thập phân(decimal) có hai phân số (vd: 1.994444 → 1.99) trong ngôn ngữ lập trình C#.
Mã nguồn - Source code:

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

namespace RoundDoubleNumbers
{
    class Program
    {
        static void Main(string[] args)
        {

            decimal a = 1.994444M;
            a = Math.Round(a, 2); //returns 1.99

            decimal b = 1.995555M;
            b = Math.Round(b, 2); //returns 2.00

            System.Console.WriteLine("a decimal: " + a);
            System.Console.WriteLine("b decimal: " + b);
            System.Console.WriteLine("\n -- VNLIVES.NET ---------");

            System.Console.ReadLine();


        }
    }
}


Kết quả - Result:





Write: +Bui Ngoc Son






No comments:

Post a Comment