-1

我正在验证我的文本框是否允许十进制值广告 12.00、12.50。我的代码是

double x;
double.TryParse(tb.Text, out x);
tb.Text = x.ToString("0.00");

它离开时会在文本框中添加小数位。因此,我想将 .00 添加到我的特定单元格的数据网格视图单元格中。谢谢

4

4 回答 4

2

DataGridView我认为您需要在单元格中显示最多 2 位小数的值

您可以尝试将列的 DefaultCellStyle 属性设置为N2(2 位小数)

dataGridView1.Columns["YourColumn"].DefaultCellStyle.Format = "N2";
于 2012-12-13T09:31:00.343 回答
0
private void dataGridView2_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
   {
       if (this.dataGridView2.Columns[e.ColumnIndex].Name == "Aidat")
       {
          string deger=(string)e.Value;
          deger = String.Format("{0:0.00}", deger);
       }
   }
于 2013-04-14T08:28:39.283 回答
0

谢谢我用dataGridView1.Columns["YourColumn"].DefaultCellStyle.Format = "N"; 这个很好而且工作。

于 2013-06-12T12:11:49.947 回答
0

上面的答案已经足够好了,但是您也可以为相同的任务设计自己的函数,下面的函数会将 23 转换为 23.00 , 23.0 到 23.00 , 23. 到 23.00 , 23.1 到 23.10 和 23.11 将保持原样,它是只是一个解释逻辑的例子

//此函数用于将浮点值格式化为小数点后两位

    private string fn_decimal_formatting(float val)
    {
        String str = val.ToString();
        int pos = str.IndexOf('.');
        if (pos == -1) //IndexOf returns negative one if . does not found in the string
            str += ".00";
        else if (str.Length == pos + 1)
            str += "00";
        else if (str.Length == pos + 2)
            str += "0";
        else
        {
            int start = 0;
            int end = pos + 2;
            str = str.Substring(start, end + 1);
        }
        return str;            
    }
于 2013-12-31T17:01:17.890 回答