vb.net 你如何在 VB .NET 中将 Double 向下舍入到最接近的整数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/378259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you round a Double down to the nearest integer in VB .NET?
提问by Steve Duitsman
How do you round a Double down to the nearest integer in VB .NET?
你如何在 VB .NET 中将 Double 向下舍入到最接近的整数?
回答by Chris Pitman
This is pretty old, but the accepted answer of using Math.Truncate
is technically incorrect: Truncate rounds towards zero, not down. For example, -1.5 is rounded to -1 instead of -2.
这已经很老了,但使用的公认答案在Math.Truncate
技术上是不正确的:截断舍入到零,而不是向下舍入。例如,-1.5 舍入为 -1 而不是 -2。
In order to always round down, use Math.Floor
.
为了始终向下取整,请使用Math.Floor
.
回答by Jon Skeet
Are we talking VB.NET or VB6? In VB.NET use Math.Truncate.
我们在谈论 VB.NET 还是 VB6?在 VB.NET 中使用Math.Truncate。
回答by Rajaram1991
This is the logic we have used:
这是我们使用的逻辑:
dim d as decimal = 1.50
dim I as int64 = convert.toint64(D)