vb.net 如何减少小数点后的数字?

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

how i can reduce the digits after the decimal point?

vb.netpointdowncast

提问by sangeen

I need only up to two decimal points.

我只需要最多两个小数点。

Dim v1, v2, v3, v4, v5, tv, rp1, rp2, rp3, rp4, rp5 As Double

Dim v1, v2, v3, v4, v5, tv, rp1, rp2, rp3, rp4, rp5 As Double

Dim Per1, Per2, Per3, Per4, per5 As Double

Dim Per1, Per2, Per3, Per4, per5 As Double

    Per1 = v1 / tv * 100

    Per2 = v2 / tv * 100

    Per3 = v3 / tv * 100

    Per4 = v4 / tv * 100

    per5 = v5 / tv * 100

It gives me values like per1=76.34393939202

它给了我像 per1=76.34393939202 这样的值

But if I use : Dim Per1, Per2, Per3, Per4, per5 As ULong It gives me 76

但是如果我使用: Dim Per1, Per2, Per3, Per4, per5 As ULong 它给了我 76

I want that it to gives me values like 76.34 but how i can do it? Please help me.

我希望它给我像 76.34 这样的值,但我该怎么做?请帮我。

回答by david

If you convert a Double to ULong it will remove all decimal places. If the problem is when you print them to screen, then you can simply use a string formatter:

如果您将 Double 转换为 ULong,它将删除所有小数位。如果问题出在将它们打印到屏幕时,那么您可以简单地使用字符串格式化程序:

Format(Per1, "0.00")

Documentation can be found here http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.strings.format.aspx

文档可以在这里找到http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.strings.format.aspx

If you want to round the numbers you can use

如果你想四舍五入你可以使用的数字

Math.Round(Per1, 2)

Round is documented here http://msdn.microsoft.com/en-us/library/75ks3aby.aspx

回合记录在这里http://msdn.microsoft.com/en-us/library/75ks3aby.aspx

Hope this helps

希望这可以帮助

回答by mbeso

in c# it looks like this

在 c# 中它看起来像这样

 string.Format("{0:0.00}", (double)Convert.ToInt32(tv) * 100 / Convert.ToInt32(v1))