大家好,下午好,我的代码有问题,而且我很难解决这个问题,希望有人帮我解决这个问题。
我有3个Textboxes
:
GrandTotal.Text
VatAmount.Text
TotalAmount.Text
和1 NumericUpDown1
作为百分比标识符。
以下是我的程序的方案,我将按下按钮,然后我的Datagridview中的某个列将计算并传输GrandTotal.Text
中的SUM(注意数字将以逗号和小数点显示(作为一个分数))然后我将按NumericUpdown1.Value
,让我说我按1次,NumericUpdown1.Value
的值 1 相当于 1%,价值变化后,VatAmount.Text
将显示GrandTotal.Text
的 1%多少,TotalAmount.Text
将显示GrandTotal.Text
和VatAmount.Text
的总和。
我的目标是用逗号和2个小数点显示数字(表现为分数)
这是我在文本框中的代码,以便数字将以逗号和小数点显示。
内部GrandTotal.Text
Private Sub grandtotal_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles grandtotal.TextChanged
Try
grandtotal.Text = FormatNumber(grandtotal.Text, 2, True, True, True)
Catch
End Try
End Sub
内部VatAmount.Text
Private Sub VatAmount_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VatAmount.TextChanged
Try
VatAmount.Text = FormatNumber(VatAmount.Text, 2, True, True, True)
Catch
End Try
End Sub
和内部TotalAmount.Text
Private Sub totalamount_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles totalamount.TextChanged
Dim dblValue As Decimal = totalamount.Text
totalamount.Text = dblValue.ToString("0,0.00", CultureInfo.InvariantCulture)
End Sub
以下是执行计算的代码,它位于NumericUpDown1.Value
Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
Dim input As Double = Decimal.Parse(grandtotal.Text)
Dim inputStringWithCommas As Decimal = input.ToString("0,0.0", CultureInfo.InvariantCulture)
grandtotal.Text = inputStringWithCommas
Dim vat As Double = Double.Parse(grandtotal.Text) * NumericUpDown1.Value * 0.01
VatAmount.Text = vat.ToString()
totalamount.Text = Decimal.Parse(grandtotal.Text) + vat.ToString("0,0.0", CultureInfo.InvariantCulture)
End Sub
我希望你理解我的解释,我只是想让它详细说明。
这是我的问题但在此之前让我给你一些示例输出。
在我的计算器中
第一个例子
150,001.50 * 1百分比(0.01)= 1,500.015
1,500.015 + 150,001.50 = 151,501.51
但在我的程序中输出是这样的。 :(
现在你看到了它,你可以看到输出中的差异,我的问题是如何使我的输出与我的计算器中的示例相同?正确的逗号和小数位数( TotalAmount.Text
中的2个小数位数和没有四舍五入的小数位数)
感谢您阅读本文,真的非常感谢您以后的帮助。 我正在使用VB.Net
答案 0 :(得分:0)
尝试使用这段代码剪切小数:
decimal d = 123.129M;
int multiplier = Convert.ToInt32(Math.Pow(10, 2));
decimal newD = Math.Truncate(d * multiplier) / multiplier;
修改强>
你可以在VB中创建一个函数是一个模块文件,具有常见的功能,如:
Public Function TrimDecimals(value As Double, maxDecimals As Integer) As Double
Dim multiplier As Integer = Convert.ToInt32(Math.Pow(10, maxDecimals))
Return Math.Truncate(value * multiplier) / multiplier
End Function
Public Function TrimDecimals(value As Decimal, maxDecimals As Integer) As Double
Dim multiplier As Integer = Convert.ToInt32(Math.Pow(10, maxDecimals))
Return Math.Truncate(value * multiplier) / multiplier
End Function
甚至更好,你可以在double和decimal上创建扩展,以获得更优雅的方法:
<Runtime.CompilerServices.Extension()>
Public Function TrimDecimals(value As Double, maxDecimals As Integer) As Double
Dim multiplier As Integer = Convert.ToInt32(Math.Pow(10, maxDecimals))
Return Math.Truncate(value * multiplier) / multiplier
End Function
<Runtime.CompilerServices.Extension()>
Public Function TrimDecimals(value As Decimal, maxDecimals As Integer) As Double
Dim multiplier As Integer = Convert.ToInt32(Math.Pow(10, maxDecimals))
Return Math.Truncate(value * multiplier) / multiplier
End Function
有一些问题但请尝试:
Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
Dim grandtotalValue As Decimal = TrimDecimals(Decimal.Parse(grandtotal.Text), 2)
grandtotal.Text = grandtotalValue.ToString("#,##0.00")
Dim vatValue As Double = TrimDecimals(grandtotalValue * NumericUpDown1.Value * 0.01, 2)
VatAmount.Text = vatValue.ToString("#,##0.00")
totalamount.Text = (grandtotalValue + vatValue).ToString("#,##0.00")
End Sub