C# 四舍五入round函數使用
C#中的round函數實際上不是真正的四舍五入函數,一般的程序設計語言的round函數也都不是四舍五入函數,而是銀行家舍入法函數,也就是“四舍六入五考慮,五后非零就進一,五后為零看奇偶,五前為偶應舍去,五前為奇要進一”
using System;public class Example { public static void Main() { double[] values = { 2.125, 2.135, 2.145, 3.125, 3.135, 3.145 }; foreach (double value in values) Console.WriteLine("{0} --> {1}", value, Math.Round(value, 2, MidpointRounding.AwayFromZero));
} } // The example displays the following output: // 2.125 --> 2.13 // 2.135 --> 2.13 // 2.145 --> 2.15 // 3.125 --> 3.13 // 3.135 --> 3.14 // 3.145 --> 3.15 </pre>
本文由用戶 pdkie 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!