[C#] 浮點數四捨五入計算

資料來自於 http://jashliao.pixnet.net/blog/post/223492905-c%23-%E6%B5%AE%E9%BB%9E%E6%95%B8%E5%9B%9B%E6%8D%A8%E4%BA%94%E5%85%A5-%5B-math.round-%5D-%E8%A8%88%E7%AE%97

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

namespace CS_Console_Round
{
    class Program
    {
        static void pause()
        {
            Console.Write(“Press any key to continue . . . “);
            Console.ReadKey(true);
        }
        static void Main(string[] args)
        {
            //https://social.msdn.microsoft.com/Forums/zh-TW/a4017834-99b9-4bde-ad16-db1ff71a564a?forum=233
            double A = 2;
            double B = 3;
            double C = A/B;
            double D = Math.Round(A / B, 2);//精度到小數點第二位,第三位四捨五入
            Console.WriteLine(“2/3={0}”, 1/3);//整數運算
            Console.WriteLine(“2/3={0}”, C);//標準浮點數
            Console.WriteLine(“2/3={0}”, D);//四捨五入浮點數
            Console.WriteLine(“2/3={0}”, C.ToString(“0.00”));//數字轉字串方法
            pause();
        }
    }
}

Comments |0|

Legend *) Required fields are marked
**) You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
Category: 來玩C#