零食天堂專做零食推薦的網(wǎng)站公關(guān)
Math.Round()并不是嚴格意義上的是四舍五入函數(shù)。它默認的執(zhí)行的是“銀行家舍入”算法,即四舍六入五取偶。概括為:四舍六入五考慮、五后非零就進一,五后皆零看奇偶,五前為偶應舍去、五前為奇要進一。
? ? ? ? 當為5時,取離著最近的偶數(shù)。見下圖:
? ? ? ? ? ? ???
測試代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MathRoundTest
{
? ? class Program
? ? {
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? Console.WriteLine(Math.Round(1234.5).ToString()); ?//輸出:1234
? ? ? ? ? ? Console.WriteLine(Math.Round(1234.50).ToString()); ?//輸出:1234
? ? ? ? ? ? Console.WriteLine(Math.Round(1235.5).ToString()); ?//輸出:1236
? ? ? ? ? ? Console.WriteLine(Math.Round(1235.50).ToString()); ?//輸出:1236
? ? ? ? ? ? Console.WriteLine("---------------------------------------------");
? ? ? ? ? ? Console.WriteLine(Math.Round(-1234.5).ToString()); ?//輸出:-1234
? ? ? ? ? ? Console.WriteLine(Math.Round(-1234.50).ToString()); ?//輸出:-1234
? ? ? ? ? ? Console.WriteLine(Math.Round(-1235.5).ToString()); ?//輸出:-1236
? ? ? ? ? ? Console.WriteLine(Math.Round(-1235.5).ToString()); ?//輸出:-1236
? ? ? ? ? ? Console.WriteLine("---------------------------------------------");
? ? ? ? ? ? Console.WriteLine(Math.Round(123.45, 1).ToString()); ?//輸出:123.4
? ? ? ? ? ? Console.WriteLine(Math.Round(123.55, 1).ToString()); ?//輸出:123.6
? ? ? ? ? ? Console.WriteLine("---------------------------------------------");
? ? ? ? ? ? Console.WriteLine(Math.Round(-123.45, 1).ToString()); ?//輸出:-123.4
? ? ? ? ? ? Console.WriteLine(Math.Round(-123.55, 1).ToString()); ?//輸出:-123.6
? ? ? ? ? ? Console.WriteLine("---------------------------------------------");
? ? ? ? ? ? Console.ReadLine();
? ? ? ? }
? ? }
}
?
如果要實現(xiàn)我們所需要的四舍五入,需要使用使用四舍五入策略參數(shù):MidpointRounding.AwayFromZero? (當為5時,取遠離0的數(shù)值)。
測試代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MathRoundTest
{
? ? class Program
? ? {
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? Console.WriteLine(Math.Round(1234.5,MidpointRounding.AwayFromZero).ToString()); ?//輸出:1235
? ? ? ? ? ? Console.WriteLine(Math.Round(1234.50, MidpointRounding.AwayFromZero).ToString()); ?//輸出:1235
? ? ? ? ? ? Console.WriteLine(Math.Round(1235.5, MidpointRounding.AwayFromZero).ToString()); ?//輸出:1236
? ? ? ? ? ? Console.WriteLine(Math.Round(1235.50, MidpointRounding.AwayFromZero).ToString()); ?//輸出:1236
? ? ? ? ? ? Console.WriteLine("---------------------------------------------");
? ? ? ? ? ? Console.WriteLine(Math.Round(-1234.5, MidpointRounding.AwayFromZero).ToString()); ?//輸出:-1235
? ? ? ? ? ? Console.WriteLine(Math.Round(-1234.50, MidpointRounding.AwayFromZero).ToString()); ?//輸出:-1235
? ? ? ? ? ? Console.WriteLine(Math.Round(-1235.5, MidpointRounding.AwayFromZero).ToString()); ?//輸出:-1236
? ? ? ? ? ? Console.WriteLine(Math.Round(-1235.5, MidpointRounding.AwayFromZero).ToString()); ?//輸出:-1236
? ? ? ? ? ? Console.WriteLine("---------------------------------------------");
? ? ? ? ? ? Console.WriteLine(Math.Round(123.45, 1, MidpointRounding.AwayFromZero).ToString()); ?//輸出:123.5
? ? ? ? ? ? Console.WriteLine(Math.Round(123.55, 1, MidpointRounding.AwayFromZero).ToString()); ?//輸出:123.6
? ? ? ? ? ? Console.WriteLine("---------------------------------------------");
? ? ? ? ? ? Console.WriteLine(Math.Round(-123.45, 1, MidpointRounding.AwayFromZero).ToString()); ?//輸出:-123.5
? ? ? ? ? ? Console.WriteLine(Math.Round(-123.55, 1, MidpointRounding.AwayFromZero).ToString()); ?//輸出:-123.6
? ? ? ? ? ? Console.WriteLine("---------------------------------------------");
? ? ? ? ? ? Console.ReadLine();
? ? ? ? }
? ? }
}
?