Vb.net 2小数加倍

时间:2014-07-13 12:20:37

标签: c# vb.net datagridview format double

我想在逗号后面将所有双打放到2位小数。 在此值为0.30的时刻,我的Datagridview显示0.3 ..任何想法?

这是我的代码:

    eenheidsprijs = Math.Round(eenheidsprijs, 2)
    basisbedrag = Math.Round(basisbedrag, 2)
    kortingbedrag = Math.Round(kortingbedrag, 2)
    netto = Math.Round(netto, 2)
    btwbedrag = Math.Round(btwbedrag, 2)
    totaal = Math.Round(totaal, 2)

    Dim streenheidsprijs, strbasisbedrag, strkortingbedrag, strnetto, strbtwbedrag, strtotaal As String

    streenheidsprijs = Format(eenheidsprijs, "0.00")
    strbasisbedrag = Format(basisbedrag, "0.00")
    strkortingbedrag = Format(kortingbedrag, "0.00")
    strnetto = Format(netto, "0.00")
    strbtwbedrag = btwbedrag.ToString("N", CultureInfo.InvariantCulture)
    strtotaal = Format(totaal, "0.00")


    eenheidsprijs = Double.Parse(streenheidsprijs)
    basisbedrag = Double.Parse(strbasisbedrag)
    kortingbedrag = Double.Parse(strkortingbedrag)
    netto = Double.Parse(strnetto)
    btwbedrag = Double.Parse(strbtwbedrag)
    totaal = Double.Parse(strtotaal)

2 个答案:

答案 0 :(得分:0)

哪个值绑定到您的数据网格? streenheidsprijs或eenheidsprijs?字符串值的格式为2位小数,但double变量不会继承要显示的小数位数。实际上,行中没有值来重新解析已格式化的字符串值:

eenheidsprijs = Double.Parse(streenheidsprijs)

数据网格列应该绑定到已格式化为2 dp的字符串值,或者您应该配置数据网格中列的显示属性,以便它们只显示两个小数位。

答案 1 :(得分:0)

您可以向format stringdefault cell style提供column。在您的情况下,格式F2将更可取。

在运行时:

myDataGridView.Columns("myColumnName").DefaultCellStyle.Format = "F2"

在设计时:

enter image description here

示例表格

Public Class Form1

    Public Sub New()
        Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US")
        Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US")
        Me.InitializeComponent()
        Me.ClientSize = New Size(350, 400)
        Me.column1 = New DataGridViewTextBoxColumn With {.Name = "Column1", .HeaderText = "Double (F2)", .ValueType = GetType(Double), .Width = 100}
        Me.column1.DefaultCellStyle.Format = "F2"
        Me.column2 = New DataGridViewTextBoxColumn With {.Name = "Column2", .HeaderText = "Double (F8)", .ValueType = GetType(Double), .AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill, .MinimumWidth = 100}
        Me.column2.DefaultCellStyle.Format = "F8"
        Me.grid = New DataGridView With {.Dock = DockStyle.Fill}
        Me.grid.Columns.AddRange(Me.column1, Me.column2)
        For i As Double = 0.0R To 9.0R : Me.grid.Rows.Add(i, i) : Next
        Me.Controls.Add(Me.grid)
    End Sub

    Private WithEvents grid As DataGridView
    Private WithEvents column1 As DataGridViewTextBoxColumn
    Private WithEvents column2 As DataGridViewTextBoxColumn

End Class

enter image description here