本文編程學(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
掃碼二維碼 獲取免費(fèi)視頻學(xué)習(xí)資料
- 本文固定鏈接: http://www.wangchenghua.com/post/10618/
- 轉(zhuǎn)載請(qǐng)注明:轉(zhuǎn)載必須在正文中標(biāo)注并保留原文鏈接
- 掃碼: 掃上方二維碼獲取免費(fèi)視頻資料