如何在 C# 中将 0.005 舍入到小数点后 3 位最大值

How to round down 0.005 to 3 decimal max in C#

我尝试在 C# 中四舍五入小数值但没有成功。

我必须四舍五入到 0,005 才能降低。逗号后的 return 值最多为 3 个数字。

我解释一下:

46.60501 应该 return => 46.605

46.09784 应该 return => 46.095

46.60434 应该 return => 46.600(而不是 46.605,因为四舍五入!)

实际上我在 c# 中使用了该代码(查看评论以了解问题所在):

//Send '46.60501' return me '46.605' => Success
double center_latitude_min_box = Math.Round( (Math.Round(latitude * 200, MidpointRounding.AwayFromZero) / 200),3);

//Send '7.09784' return me '7.1' ?!? => No Success, and why only return 1 number after the comma ?
double center_longitude_min_box = Math.Round((Math.Round(longitude * 200, MidpointRounding.AwayFromZero) / 200), 3);

但效果不佳:'(

如果有人能帮助我,我将不胜感激!我开始疯狂了^^

你可以使用类似

的东西
var x  = 0;
if((latitude*100) - Math.Truncate((latitude*100)) >=0.5)
{
x = (Math.Truncate((latitude*100))/100)+0.005;
}
else
{
x = (Math.Truncate((latitude*100))/100);
}

编辑 ***** 编辑 下面古斯塔夫的解决方案要好得多

This can really be done directly in one line:

x = Math.Floor(latitude * 200) / 200;

这真的可以直接一行完成:

x = Math.Floor(latitude * 200) / 200;