本文介绍了如何在C#中的数据集中设置十进制值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据集中有一列AMOUNT_PAISE。我从来自sql server存储过程的结果集绑定此数据集。对于数据库货币的AMOUNT_PAISE列为paise(对于特定行可以说为888 paise),但是我想在Rupees中显示此数量(Rupee等价于888 paise为8.88)。但是我不能直接分配它,因为数据集包含int64值。当我将十进制888转换成int64时,它给出9(888/100 = 8.88,当转换为int64时,它给出9)
所以请告诉我现在应该尝试什么。

I have a Column AMOUNT_PAISE in Dataset. I'm binding this dataset from the result set coming from sql server stored procedure. for AMOUNT_PAISE column in DB currency is in paise(lets say 888 paise for a particular row) but I want to display this amount in Rupees(Rupee equivalenof 888 paise is 8.88). But I can not directly assign it, because dataset contains int64 values. when I convert decimal 888 to int64 it gives 9 (888/100 = 8.88 and when converted to int64 it gives 9)So Please tell me what should I try now.

从paise转换为卢比的公式。

formula to convert from paise to rupee.

rupee = paise/ 100; //upto 2 decimal points

if (dsTransactionLogs.Tables.Count > 0)
{
    if (dsTransactionLogs.Tables[0].Rows.Count > 0)
    {

        //Code to convert amount from Paise to Rs.
        int i = 0;
        foreach (DataRow row in dsTransactionLogs.Tables[0].Rows)
        {
            decimal decimalAmountInPaise = Convert.ToDecimal(dsTransactionLogs.Tables[0].Rows[i]["AMOUNT_PAISE"]);
            decimalAmountInPaise = Math.Round(decimalAmountInPaise / 100, 2);
            //Int64 int64AmountInPaise = Convert.ToInt64(decimalAmountInPaise);
            string strAmount = decimalAmountInPaise.ToString("#.##");
            dsTransactionLogs.Tables[0].Rows[i]["AMOUNT_PAISE"] = Convert.ToInt64(strAmount);
            i++;

        }

        GridViewTRANSACTIONDETAILS.DataSource = dsTransactionLogs;
        GridViewTRANSACTIONDETAILS.DataBind();
        transactionGVDiv.Style.Add("height", "400px");
        //LabelNoTransactionLogsID.Visible = false;
    }
    else
    {
        //LabelNoTransactionLogsID.Visible = true;
    }
}

这是gridview中此列的aspx代码。 / p>

this is aspx code for this column in gridview.

<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="Amount"> 
<ItemTemplate> 
    <asp:Label runat="server" ID="AmountLbl" NavigateUrl="#" Text='<%#Eval("AMOUNT_PAISE") %>'> ></asp:Label>
</ItemTemplate>
</asp:TemplateField>


推荐答案

我建议您在显示时进行转换,而不是尝试修改DataTable。即替换:

I suggest you do the conversion when displaying, rather than attempting to modify the DataTable. I.e. replace:

... Text='<%#Eval("AMOUNT_PAISE") %>'> ...

如下:

... Text='<%#  (Convert.ToDecimal(Eval("AMOUNT_PAISE")) / 100M).ToString("N2") %>'> ...

这篇关于如何在C#中的数据集中设置十进制值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 16:12