編程學(xué)習(xí)網(wǎng) > 編程語(yǔ)言 > C/C++開發(fā) > c語(yǔ)言中round函數(shù)四舍五入怎么用?(C# 四舍五入round函數(shù)使用)
2023
02-15

c語(yǔ)言中round函數(shù)四舍五入怎么用?(C# 四舍五入round函數(shù)使用)

本文編程學(xué)習(xí)網(wǎng)主要和大家介紹C語(yǔ)言中round函數(shù)四舍五入怎么用?,通過(guò)具體的代碼向大家展示,希望對(duì)大家學(xué)習(xí)C語(yǔ)言編程入門有所幫助。

C#中的round函數(shù)實(shí)際上不是真正的四舍五入函數(shù),一般的程序設(shè)計(jì)語(yǔ)言的round函數(shù)也都不是四舍五入函數(shù),而是銀行家舍入法函數(shù),也就是“四舍六入五考慮,五后非零就進(jìn)一,五后為零看奇偶,五前為偶應(yīng)舍去,五前為奇要進(jìn)一”

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

以上就是“c語(yǔ)言中round函數(shù)四舍五入怎么用?”的詳細(xì)內(nèi)容,想要了解更多C語(yǔ)言教程歡迎持續(xù)關(guān)注編程學(xué)習(xí)網(wǎng)!

掃碼二維碼 獲取免費(fèi)視頻學(xué)習(xí)資料

Python編程學(xué)習(xí)

查 看2022高級(jí)編程視頻教程免費(fèi)獲取