簡體   English   中英

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

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

我正在使用VB.Net制作一個asp.net網頁,該網頁計算員工的年薪。

我有一個GridView,其顯示的列的值類型為double(在數據庫“示例:66,556.91”中它們為double)。

我想四舍五入此列的值,以便我有66557,而不是66556.91。 我檢查了msdn ,並在gridview任務的DataFormatString中使用了{0:D}{0:F0} -編輯列,但未更改任何值。

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

你有什么建議?

像這樣為薪水列更新了選擇命令,它將在數據庫末尾執行舍入運算。

SELECT ROUND(SalaryColumnName,0)

您可以在{0:f0}的綁定列定義中提供DataFormatString,該值會將您的值四舍五入到小數點后0位。

輸入值

1
2.2
3.323
66556.91

格式化值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