python中round函数的一个小坑——奇进偶弃

python中round函数的一个小坑——奇进偶弃

 

>>> round(3.1415,3)
3.142
>>> round(4.1415,3)
4.141

 

>>> round(10.5)
10
>>> round(11.5)
12

 

 

C#中也有类似的设计

https://learn.microsoft.com/en-us/dotnet/api/system.midpointrounding?view=net-7.0

Fields

AwayFromZero 1

The strategy of rounding to the nearest number, and when a number is halfway between two others, it's rounded toward the nearest number that's away from zero.

ToEven 0

The strategy of rounding to the nearest number, and when a number is halfway between two others, it's rounded toward the nearest even number.

ToNegativeInfinity 3

The strategy of downwards-directed rounding, with the result closest to and no greater than the infinitely precise result.

ToPositiveInfinity 4

The strategy of upwards-directed rounding, with the result closest to and no less than the infinitely precise result.

ToZero 2

The strategy of directed rounding toward zero, with the result closest to and no greater in magnitude than the infinitely precise result.

 

Remarks

Use MidpointRounding with appropriate overloads of Math.Round, MathF.Round, and Decimal.Round to provide more control of the rounding process.

There are two overall rounding strategies, round to nearest and directing rounding, and each enumeration field participates in exactly one of these strategies.

Round to nearest

Fields:

A round-to-nearest operation takes an original number with an implicit or specified precision; examines the next digit, which is at that precision plus one; and returns the nearest number with the same precision as the original number. For positive numbers, if the next digit is from 0 through 4, the nearest number is toward negative infinity. If the next digit is from 6 through 9, the nearest number is toward positive infinity. For negative numbers, if the next digit is from 0 through 4, the nearest number is toward positive infinity. If the next digit is from 6 through 9, the nearest number is toward negative infinity.

If the next digit is from 0 through 4 or 6 through 9, the MidpointRounding.AwayFromZero and MidpointRounding.ToEven do not affect the result of the rounding operation. However, if the next digit is 5, which is the midpoint between two possible results, and all remaining digits are zero or there are no remaining digits, the nearest number is ambiguous. In this case, the round-to-nearest modes in MidpointRounding enable you to specify whether the rounding operation returns the nearest number away from zero or the nearest even number.

The following table demonstrates the results of rounding some negative and positive numbers in conjunction with round-to-nearest modes. The precision used to round the numbers is zero, which means the number after the decimal point affects the rounding operation. For example, for the number -2.5, the digit after the decimal point is 5. Because that digit is the midpoint, you can use a MidpointRounding value to determine the result of rounding. If AwayFromZero is specified, -3 is returned because it is the nearest number away from zero with a precision of zero. If ToEven is specified, -2 is returned because it is the nearest even number with a precision of zero.

Original number AwayFromZero ToEven
3.5 4 4
2.8 3 3
2.5 3 2
2.1 2 2
-2.1 -2 -2
-2.5 -3 -2
-2.8 -3 -3
-3.5 -4 -4

Directed rounding

Fields:

A directed-rounding operation takes an original number with an implicit or specified precision and returns the next closest number in a specific direction with the same precision as the original number. Directed modes on MidpointRounding control toward which predefined number the rounding is performed.

The following table demonstrates the results of rounding some negative and positive numbers in conjunction with directed-rounding modes. The precision used to round the numbers is zero, which means the number before the decimal point is affected by the rounding operation.

Original number ToNegativeInfinity ToPositiveInfinity ToZero
3.5 3 4 3
2.8 2 3 2
2.5 2 3 2
2.1 2 3 2
-2.1 -3 -2 -2
-2.5 -3 -2 -2
-2.8 -3 -2 -2
-3.5 -4 -3 -3

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(169)  评论(2编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2021-09-24 Anti forgery token is meant for user "" but the current user is "username"
2021-09-24 Asp.net Mvc AntiForgeryToken全局处理
2021-09-24 Does it make sense to put antiforgerytoken in _Layout.cshtml?
2021-09-24 jQuery .val()
2021-09-24 Understanding multiple anti-forgery tokens in ASP.NET MVC
2021-09-24 Duplicate Files Search & Link
2021-09-24 The source contains no DataRows
点击右上角即可分享
微信分享提示