vb.net 如何将答案转换为两位小数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16581901/
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 to convert answer into two decimal point
提问by Husna5207
This is my code and I want the output which is txtA.Text
and txtB.Text
to be in two decimal places.
这是我的代码,我希望输出是txtA.Text
和txtB.Text
小数点后两位。
Public Class Form1
Private Sub btncalc_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btncalc.Click
txtA.Text = Val(txtD.Text) / Val(txtC.Text) * Val(txtF.Text) / Val(txtE.Text)
txtB.Text = Val(txtA.Text) * 1000 / Val(txtG.Text)
End Sub
End Class
回答by fnostro
回答by Andrea
Try using the Format
function:
尝试使用该Format
功能:
Private Sub btncalc_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btncalc.Click
txtA.Text = Format(Val(txtD.Text) / Val(txtC.Text) *
Val(txtF.Text) / Val(txtE.Text), "0.00")
txtB.Text = Format(Val(txtA.Text) * 1000 / Val(txtG.Text), "0.00")
End Sub
回答by Douglas Barbin
If you have a Decimal or similar numeric type, you can use:
如果您有 Decimal 或类似的数字类型,则可以使用:
Math.Round(myNumber, 2)
EDIT: So, in your case, it would be:
编辑:所以,在你的情况下,它将是:
Public Class Form1
Private Sub btncalc_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btncalc.Click
txtA.Text = Math.Round((Val(txtD.Text) / Val(txtC.Text) * Val(txtF.Text) / Val(txtE.Text)), 2)
txtB.Text = Math.Round((Val(txtA.Text) * 1000 / Val(txtG.Text)), 2)
End Sub
End Class
回答by vicsar
Call me lazy but:
叫我懒惰但是:
lblTellBMI.Text = "Your BMI is: " & Math.Round(sngBMI, 2)
I.e.: Label lblTellBMIwill display Your BMI is:and then append the value from a Singletype variable (sngBMI) as 2 decimal points, simply by using the Math.Roundmethod.
即:标签lblTellBMI将显示您的 BMI 是:然后将来自单一类型变量 ( sngBMI)的值附加为 2 个小数点,只需使用Math.Round方法即可。
The Math.Roundmethod rounds a value to the nearest integer or to the specified number of fractional digits.
该Math.Round方法舍入的值到最接近的整数或小数位指定数目。
Source: https://msdn.microsoft.com/en-us/library/system.math.round(v=vs.110).aspx
来源:https: //msdn.microsoft.com/en-us/library/system.math.round(v=vs.110).aspx
回答by SEFL
Ran into this problem today and I wrote a function for it. In my particular case, I needed to make sure all values were at least 0 (hence the "LT0" name) and were rounded to two decimal places.
今天遇到这个问题,我为它写了一个函数。在我的特殊情况下,我需要确保所有值至少为 0(因此是“LT0”名称)并四舍五入到小数点后两位。
Private Function LT0(ByVal Input As Decimal, Optional ByVal Precision As Int16 = 2) As Decimal
' returns 0 for all values less than 0, the decimal rounded to (Precision) decimal places otherwise.
If Input < 0 Then Input = 0
if Precision < 0 then Precision = 0 ' just in case someone does something stupid.
Return Decimal.Round(Input, Precision) ' this is the line everyone's probably looking for.
End Function
回答by manuel
If you just want to print a decimal number with 2 digits after decimal point in specific format no matter of locals use something like this
如果您只想以特定格式打印小数点后 2 位的十进制数,无论本地人如何,请使用这样的方法
dim d as double = 1.23456789
dim s as string = d.Tostring("0.##", New System.Globalization.CultureInfo("en-US"))