You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Infragistics UltraWinGrid大数值单元格显示四舍五入异常问题

问题描述

在Infragistics UltraWinGrid的金额单元格中输入大数值999999999999999.99时,网格显示被四舍五入为1,000,000,000,000,000.00,但数据库保存的数值是正确的。调试发现MyGrid_BeforeCellActivated事件中activeCell.Value为正确的999999999999999.99,但只读的activeCell.Text是四舍五入后的结果。相关代码如下:

Dim UltraGridColumn1 As Infragistics.Win.UltraWinGrid.UltraWinColumn = New Infragistics.Win.UltraWinGrid.UltraWinColumn("MyAmount")

Private Sub MyGrid_Initializelayout(...) Handles MyGrid.Initializelayout
    With e.Layout
        .Bands(0).Columns("MyAmount").Editor = AmountEditor
        .Bands(0).Columns("MyAmount").MaskInput = "{double:15.2:c}"
        .Bands(0).Columns("MyAmount").Format = "###,###,###,###,##0.00"
    End With
End Sub

Private Sub myGrid_AfterExitEditMode(ByVal sender As Object, ByVal a As System.EventArgs) Handles myGrid.AfterExitEditMode
    MyGrid.ActiveRow.Cells("MyAmount").Column.MaskInput = "nnn,nnn,nnn,nnn,nnn.nn"
End Sub

AmountEditor = New EditorWithMask
AmountEditor.DataFilter = New FormatAmountConverter()

Public Class FormatAmountConverter Implements IEditorDataFilter
    Public Function Convert(ByVal myArgs As Infragistics.Win.EditorDataFilterConvertArgs) As Object Implements Infragistics.Win.IEditorDataFilter.Convert
        'Some Logic
        Return CType(myArgs.Value, Double).ToString("N02")
    End Function
End Class  
解决方案

核心原因

Double类型的精度限制导致显示时四舍五入。Double的有效精度约为15-17位,999999999999999.99已经接近其精度上限,转换为字符串时会被近似为1000000000000000。

具体修复方案

1. 替换数据类型为Decimal(推荐)

Decimal类型支持最高28-29位有效数字,能精准存储此类大金额数值,从根源解决精度丢失问题:

  • 确保单元格绑定的数据源字段类型为Decimal;
  • 调整代码中所有涉及数值转换的部分为Decimal。

2. 调整网格列的格式与掩码配置

修改列的MaskInput为适配Decimal的格式,避免Double类型的转换问题:

Private Sub MyGrid_Initializelayout(...) Handles MyGrid.Initializelayout
    With e.Layout
        .Bands(0).Columns("MyAmount").Editor = AmountEditor
        ' 将double改为decimal,掩码位数适配大数值
        .Bands(0).Columns("MyAmount").MaskInput = "{decimal:16.2:c}"
        .Bands(0).Columns("MyAmount").Format = "###,###,###,###,###0.00"
    End With
End Sub

Private Sub myGrid_AfterExitEditMode(ByVal sender As Object, ByVal a As System.EventArgs) Handles myGrid.AfterExitEditMode
    MyGrid.ActiveRow.Cells("MyAmount").Column.MaskInput = "nnn,nnn,nnn,nnn,nnn.nn"
End Sub

3. 修改数据过滤器的类型转换

更新FormatAmountConverter中的转换逻辑,使用Decimal而非Double:

Public Class FormatAmountConverter Implements IEditorDataFilter
    Public Function Convert(ByVal myArgs As Infragistics.Win.EditorDataFilterConvertArgs) As Object Implements Infragistics.Win.IEditorDataFilter.Convert
        'Some Logic
        Return CType(myArgs.Value, Decimal).ToString("N02")
    End Function
End Class

4. 事件中强制修正显示文本(临时兼容方案)

如果无法立即替换数据源类型,可以在AfterExitEditMode事件中手动设置单元格的显示文本,直接基于Value的精确值格式化:

Private Sub myGrid_AfterExitEditMode(ByVal sender As Object, ByVal e As System.EventArgs) Handles myGrid.AfterExitEditMode
    Dim targetCell = MyGrid.ActiveRow.Cells("MyAmount")
    If targetCell.Value IsNot Nothing Then
        ' 强制用Decimal转换后格式化,避免Double的精度丢失
        targetCell.Text = CType(targetCell.Value, Decimal).ToString("###,###,###,###,###0.00")
    End If
End Sub

内容的提问来源于stack exchange,提问作者Sonali bhosale

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 08:23:29