c#添加浮点数行为奇怪 [英] c# adding float numbers behave weirdly

查看:205
本文介绍了c#添加浮点数行为奇怪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试添加行的单元格,并将其显示在网格中的新单元格中。我在DevExpress中使用gridControl。网格有12列表示一年中的月份,我想添加月份值,并在第13列显示总计。

I am trying to add a row's cells and display the total in a new cell in a grid. I am using gridControl from DevExpress. The grid has 12 columns representing the months in a year and I want to add the months values and display the total in the 13th column.

我的问题是,如果我有浮点值为111,22,它被添加为数据库111,2188,并在第13个单元格中显示为精度。

My problem is that if I have a float value of "111,22", it is added as "111,2188" to the database and displayed without the precision in the 13th cell.

所以,我有两个问题。第一个是为什么把它作为111,2188插入数据库?第二个是为什么显示总数。

So, I have two problems. First one is "why does it insert it as 111,2188 to database?" and the second one is "why does it display the total".

这是我的代码...这发生在我添加111,2188和200,我得到211,2188 ....

here is my code... this happens when I add 111,2188 and 200, and I get 211,2188 ....

private void calculate_gv_row_total(int row_index) {
    float total = 0;
    for (int j = 0; start_index + j < start_index + months; j++)
    {
        float f = float.Parse(gv.GetDataRow(row_index)[start_index + j].ToString());
        total = total + f;
    }
    gv.GetDataRow(row_index)[total_cell_index] = total;

}

我该如何解决这些问题?我做错了什么?

how can I solve these problems? What am I doing wrong?

推荐答案

实际上,你的网格是有效的,它舍弃了111,2188到111,22。如果要查看确切的值,请增加列宽或告诉网格的精度级别。

Actually, Your Grid is efficient, it rounds off 111,2188 to 111,22. if you want to see the exact value, increase the column width or tell the grid the level of precision.

如果不需要太多的精度使用 ToString(F)

If you don't need that much precision use ToString("F")

private void calculate_gv_row_total(int row_index) {
    float total = 0;
    for (int j = 0; start_index + j < start_index + months; j++)
    {
        float f = float.Parse(gv.GetDataRow(row_index)[start_index + j].ToString("F"));
        total = total + f;
    }
    gv.GetDataRow(row_index)[total_cell_index] = total;

}

这篇关于c#添加浮点数行为奇怪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆