简体   繁体   English

如何在DataGridView VB.net中四舍五入一列

[英]How to round a column in a DataGridView VB.net

I am making an asp.net webpage using VB.Net that calculates the employee's yearly salary. 我正在使用VB.Net制作一个asp.net网页,该网页计算员工的年薪。

I have a GridView that displays a column with values of type double (they are double in the database "example: 66,556.91"). 我有一个GridView,其显示的列的值类型为double(在数据库“示例:66,556.91”中它们为double)。

I want to round the values of this column so that I have 66,557 instead of 66,556.91. 我想四舍五入此列的值,以便我有66557,而不是66556.91。 I checked msdn , and used {0:D} and {0:F0} in the DataFormatString in the gridview taks - edit columns but no value was changed. 我检查了msdn ,并在gridview任务的DataFormatString中使用了{0:D}{0:F0} -编辑列,但未更改任何值。

<asp:BoundField DataField="C1" HeaderText="calculated taxes" ReadOnly="True" SortExpression="C1" DataFormatString="{0:F0}" />

What do you suggest? 你有什么建议?

像这样为薪水列更新了选择命令,它将在数据库末尾执行舍入运算。

SELECT ROUND(SalaryColumnName,0)

You can supply a DataFormatString in your boundcolumn definition of {0:f0} which will round your values to 0 decimal places. 您可以在{0:f0}的绑定列定义中提供DataFormatString,该值会将您的值四舍五入到小数点后0位。

Input Values 输入值

1
2.2
3.323
66556.91

Formatted Values 1 2 3 66557 格式化值1 2 3 66557

Module Module1
  Sub Main()
  ' Call Math.Round on this Double.
Dim before As Double = 123.45
Dim after1 As Double = Math.Round(before, 1, MidpointRounding.AwayFromZero)
Dim after2 As Double = Math.Round(before, 1, MidpointRounding.ToEven)
Dim after3 As Double = Math.Round(before)

Console.WriteLine(before)
Console.WriteLine(after1)
Console.WriteLine(after2)
Console.WriteLine(after3)
Console.WriteLine()

  ' Use on this Decimal.
Dim before2 As Decimal = 125.101
Dim after4 As Decimal = Math.Round(before2)
Dim after5 As Decimal = Math.Round(before2, 1)

Console.WriteLine(before2)
Console.WriteLine(after4)
Console.WriteLine(after5)
End Sub
End Module

Output

123.45
123.5
123.4
123

125.101
125
125.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM