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

查看:39
本文介绍了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?

推荐答案

实际上,Your Grid 是高效的,它将 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天全站免登陆