(vb.net) 四舍五入到小数点后两位

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40740838/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:13:46  来源:igfitidea点击:

(vb.net) rounding to 2 decimal places

vb.netvariablesdecimal

提问by Mr Beanstar

I am creating a program for a project that will help me learn visual basic. But when I work out how much a certain material is worth with dimensions. It come out with more than two decimal places. I know how to use math.floor function but it doesn't round to 2 decimal places.

我正在为一个项目创建一个程序,它将帮助我学习视觉基础。但是当我计算出某种材料在尺寸上的价值时。它出来的小数点多于两位。我知道如何使用 math.floor 函数,但它不会四舍五入到小数点后两位。

Does anyone know what function I should use to round to two decimal places?

有谁知道我应该使用什么函数来四舍五入到两位小数?

采纳答案by Koen

The Decimal.Roundmethod will do the trick for you. You can specify the number of decimal places.

Decimal.Round方法就可以了你。您可以指定小数位数。

回答by Tom Johnson

You can use;

您可以使用;

Math.Round([DECIMAL], 2)

If you want to round up you can use;

如果你想四舍五入,你可以使用;

MidpointRounding.AwayFromZero

As an additional parameter to make;

作为一个额外的参数;

Math.Round([DECIMAL], 2, MidpointRounding.AwayFromZero)

Hope it helps!

希望能帮助到你!