Math.Round 在 vb.net 中没有按预期工作
Math.Round in vb.net not working as expected
我有这个数字:666872700
我需要将它四舍五入为:666900000
我用过:Math.Round(666872700,4)
但这不起作用
除了除以 100000
然后四舍五入然后乘以 100000
之外,我可以使用 vb.net 中的任何简单方法吗?
Math.Round
的 documentation 明确指出:
Rounds a double-precision floating-point value to a specified number of fractional digits.
因此它舍入了小数点后面的内容,但不是整数部分。除了除法、四舍五入然后再乘法,我没有别的办法了。
如果你懂一点 C#,你可以使用 Jason Larke 在他对 this question 的回答中写的以下扩展方法。我不知道它是否有效,但你应该可以将它翻译成 VB.NET 并尝试一下:
public static class MathExtensions
{
public static int Round(this int i, int nearest)
{
if (nearest <= 0 || nearest % 10 != 0)
throw new ArgumentOutOfRangeException("nearest", "Must round to a positive multiple of 10");
return (i + 5 * nearest / 10) / nearest * nearest;
}
}
我有这个数字:666872700
我需要将它四舍五入为:666900000
我用过:Math.Round(666872700,4)
但这不起作用
除了除以 100000
然后四舍五入然后乘以 100000
之外,我可以使用 vb.net 中的任何简单方法吗?
Math.Round
的 documentation 明确指出:
Rounds a double-precision floating-point value to a specified number of fractional digits.
因此它舍入了小数点后面的内容,但不是整数部分。除了除法、四舍五入然后再乘法,我没有别的办法了。
如果你懂一点 C#,你可以使用 Jason Larke 在他对 this question 的回答中写的以下扩展方法。我不知道它是否有效,但你应该可以将它翻译成 VB.NET 并尝试一下:
public static class MathExtensions
{
public static int Round(this int i, int nearest)
{
if (nearest <= 0 || nearest % 10 != 0)
throw new ArgumentOutOfRangeException("nearest", "Must round to a positive multiple of 10");
return (i + 5 * nearest / 10) / nearest * nearest;
}
}