为什么 C# 轮和 SQL 轮函数会产生不同的输出? [英] Why C# round and SQL round functions yields different outputs?

查看:36
本文介绍了为什么 C# 轮和 SQL 轮函数会产生不同的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 C# 和 SQL 中的 ROUND 函数,令人惊讶的是两者都产生了不同的结果.

I am using ROUND function from C# and SQL, and surprisingly both are yielding different results.

在 SQL 中:ROUND(1250.00, -2) = 1300

In SQL: ROUND(1250.00, -2) = 1300

在 C# 中 ROUND 1250 与舍入和 precision = 2 = 1200

In C# ROUND 1250 with round and precision = 2 = 1200

有没有人遇到过这种情况?

Has anyone came across this situation before?

推荐答案

默认情况下,C# 使用银行家的舍入,当您正好在 .5 标记上时,它会舍入到最接近的偶数,而不是总是向上舍入.

C# uses banker's rounding by default, where when you're exactly on the .5 mark, it rounds to the nearest even number instead of always rounding up.

msdn 文章的备注部分描述了这种行为.基本上就是把很多四舍五入的数字累积在一起的时候,减少四舍五入的错误.

The remarks section of the msdn article describes this behavior. Basically it's to reduce rounding errors when you accumulate a lot of rounded numbers together.

这篇关于为什么 C# 轮和 SQL 轮函数会产生不同的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆