Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
636 views
in Technique[技术] by (71.8m points)

c# - Format Decimal number to Indian Currency Format

I am using C# asp.net 4.0 in my project where i need to display price in India curreny format.

eg. my number is 12550000.00 then i want to display it as 1,25,50,000.00

But i want this to be displayed in gridview when i bind the data to the gridview,

so it can be done in markup page. where we place Eval for each Item Data Bound.

However, i would also like to explain my senario for displaying comma sepearted values.

i have a set of textboxes above the gridview where user makes entries of values and click add.

this gets add in the viewstate and the viewstate is binded to gridview.

In gridview i also have Edit button on click of it the values in viewstate is passed back to textbox on RowCommand Event of gridview. and on update click the viewstate datatable is updated and Binded back to gridview.

FOR your reference:

protected void gvPropertyConfig_RowCommand(object sender, GridViewCommandEventArgs e)
{
    try
    {
        if (e.CommandName == "EditItem")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            hdnIndex.Value = index.ToString();
            DataTable dt = (DataTable)ViewState["proeprtyConfig"];
            DataRow dr = dt.Rows[index];

            if (Request.QueryString["CMD"] == "Edit")
            {
                hdnPropertyConfigID.Value = dr["config_id"].ToString();
                if (dr["is_active"].ToString().ToLower() == "true")
                {
                    chkConfigVisible.Checked = true;
                }
                else
                {
                    chkConfigVisible.Checked = false;
                }
                thIsActHed.Visible = true;
                tdIsActchk.Visible = true;
                tdbtnConfig.ColSpan = 2;
            }

            txtScalableArea.Text = dr["scalable_area"].ToString();
            txtCarpetArea.Text = dr["carpet_area"].ToString();
            txtPricePerSqFt.Text = dr["price_per_sq_ft"].ToString();
            txtCCPricePerSqFt.Text = dr["cc_price_per_sq_ft"].ToString();
            txtTotalPrice.Text = dr["total_price"].ToString();
            ddlNoOfBedrooms.SelectedValue = dr["room_id"].ToString();

            btnUpdateConfig.Visible = true;
            btnConfigSubmit.Visible = false;

        }
        if (e.CommandName == "DeleteItem")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            DataTable dt = (DataTable)ViewState["proeprtyConfig"];
            DataRow dr = dt.Rows[index];
            if (Request.QueryString["CMD"].ToString() == "Edit")
            {
                int PropertyConfigID = Convert.ToInt32(dr[0].ToString());
                prConfigObj.deletePropertyConfig(PropertyConfigID);
                fillData();

            }
            else
            {
                dr.Delete();
                gvPropertyConfig.DataSource = (DataTable)ViewState["proeprtyConfig"];
                gvPropertyConfig.DataBind();
            }
            clearConfigTextBoxes();
            btnConfigSubmit.Visible = true;
            btnUpdateConfig.Visible = false;


        }
        setChecklistAttr();
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

Below is the markup for Gridview,

 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <div class="tabBord">
                <table>
                    <tr>
                        <td colspan="4" class="middle">
                            <h4>
                                Property Config Information</h4>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="4">
                            <p>
                                Note: Enter total prices in lacs only. Eg. If 1 Crore than enter 1,00,00,000
                            </p>
                            <p>
                            </p>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <div id="divconfigstr" runat="server">
                                Configuration<span style="color: Red">*</span></div>
                            <%--class="displaynon"--%>
                        </td>
                        <td>
                            <div id="divnoofbedrooms" runat="server">
                                <asp:DropDownList Enabled="false" ID="ddlNoOfBedrooms" runat="server">
                                </asp:DropDownList>
                                <p>
                                </p>
                            </div>
                        </td>
                        <td>
                            Scalable Area <span style="color: Red">*</span>
                        </td>
                        <td>
                            <asp:TextBox ID="txtScalableArea" runat="server" autocomplete="off" MaxLength="10"></asp:TextBox><p>
                            </p>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Carpet Area <span style="color: Red">*</span>
                        </td>
                        <td>
                            <asp:TextBox ID="txtCarpetArea" runat="server" autocomplete="off" MaxLength="10"></asp:TextBox><p>
                            </p>
                        </td>
                        <td>
                            Price/Sq.Ft.<span style="color: Red">*</span>
                        </td>
                        <td>
                            <asp:TextBox ID="txtPricePerSqFt" runat="server" autocomplete="off" MaxLength="10"></asp:TextBox><p>
                            </p>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            CC Price/Sq.Ft.<span style="color: Red">*</span>
                        </td>
                        <td>
                            <asp:TextBox ID="txtCCPricePerSqFt" runat="server" autocomplete="off" MaxLength="10"></asp:TextBox><p>
                            </p>
                        </td>
                        <td>
                            Total Price (in lacs)<span style="color: Red">*</span>
                        </td>
                        <td>
                            <asp:TextBox ID="txtTotalPrice" runat="server" autocomplete="off" MaxLength="10"></asp:TextBox><p>
                            </p>
                        </td>
                    </tr>
                    <tr>
                        <td id="thIsActHed" runat="server">
                            Active
                            <asp:HiddenField ID="hdnPropertyConfigID" runat="server" />
                            <asp:HiddenField ID="hdnIndex" runat="server" />
                        </td>
                        <td id="tdIsActchk" runat="server">
                            <asp:CheckBox ID="chkConfigVisible" runat="server" CssClass="checklist" /><p>
                            </p>
                        </td>
                        <td id="tdbtnConfig" runat="server" colspan="2">
                            <div class="btnHold">
                                <asp:Button ID="btnConfigSubmit" runat="server" Text="Add" OnClientClick="return ValidatePropertyConfig();"
                                    CssClass="sendBtn" OnClick="btnConfigSubmit_Click" />
                                &nbsp;
                                <asp:Button ID="btnUpdateConfig" runat="server" OnClick="btnUpdateConfig_Click" OnClientClick="return ValidatePropertyConfig();"
                                    CssClass="sendBtn" Text="Update" Visible="False" />
                                <asp:Label ID="lblerrconfig" CssClass="errormsg" runat="server"></asp:Label>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="4">
                            <div class="pHold">
                                <div class="gridH">
                                    <asp:GridView ID="gvPropertyConfig" runat="server" AutoGenerateColumns="False" OnRowCommand="gvPropertyConfig_RowCommand"
                                        OnRowDataBound="gvPropertyConfig_RowDataBound">
                                        <Columns>
                                            <asp:TemplateField HeaderText="No Of Bedrooms" ItemStyle-CssClass="txtLT">
                                                <ItemTemplate>
                                                    <asp:Label ID="lblno_of_bedrooms" runat="server" Text='<%# Eval("room_no") %>'></asp:Label>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Scalable Area" ItemStyle-CssClass="txtRT">
                                                <EditItemTemplate>
                                                    <asp:TextBox ID="txtscalable_area" runat="server" Text='<%# Eval("scalable_area") %>'></asp:TextBox>
                                                </EditItemTemplate>
                                                <ItemTemplate>
                                                    <asp:Label ID="lblscalable_area" runat="server" Text='<%# Eval("scalable_area") %>'></asp:Label>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Carpet Area" ItemStyle-CssClass="txtRT">
                                                <EditItemTemplate>
                                                    <asp:TextBox ID="txtcarpet_area" runat="server" Text='<%# Eval("carpet_area") %>'></asp:TextBox>
                                                </EditItemTemplate>
                                                <ItemTemplate>
                                                    <asp:Label ID="lblcarpet_area" runat="server" Text='<%# Eval("carpet_area") %>'></asp:Label>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Price/SqFt." ItemStyle-CssClass="txtRT">
                                                <EditItemTemplate>
                                                    <asp:TextBox ID="txtprice_per_sq_ft" runat="server" Text='<%# Eval("price_per_sq_ft") %>'></asp:TextBox>
                                                </Edi

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use the "C" parameter in the ToString function, and make sure you set the globalaztion attributes.

string parseValueIntoCurrency(double number) {
   // set currency format
   string curCulture = Thread.CurrentThread.CurrentCulture.ToString();
   System.Globalization.NumberFormatInfo currencyFormat = new 
       System.Globalization.CultureInfo(curCulture).NumberFormat;

   currencyFormat.CurrencyNegativePattern = 1;

   return number.ToString("c", currencyFormat);
}

If you want to use a different Culture (say you're in USA, and you want the Indian format) then just use the appropriate CultureInfo element rather than getting it out of the current thread.

EXTRA INFO DUE TO OP EDIT

All right, what you're wanting to do to get this into your grid, is to create a PROTECTED function which takes in the number to be converted, and returns the converted string (this is basically the code above.

Now, on the ASPX side, you need to use that function in your grid view.
So, instead of this:

 <asp:TemplateField HeaderText="Total Price (in lacs)" >
   <EditItemTemplate>
      <asp:TextBox ID="txttotal_price" runat="server" 
                   Text='<%# Eval("total_price") %>' />
    </EditItemTemplate>
    <ItemTemplate>
       <asp:Label ID="lbltotal_price" runat="server" 
                  Text='<%# Eval("total_price") %>'> />
    </ItemTemplate>
 </asp:TemplateField>

you'll use this templatefield:

  <asp:TemplateField HeaderText="Total Price (in lacs)" >
    <EditItemTemplate>
       <asp:TextBox ID="txttotal_price" runat="server" 
                    Text='<%# Eval("total_price") %>' />
    </EditItemTemplate>
    <ItemTemplate>
        <%# parseValueIntoCurrency(Eval("total_price")) %>'>
     </ItemTemplate>
 </asp:TemplateField>

Note, two things. The first is that I'm still passing the UNFORMATTED value into the EDIT TEMPLATE, and that I'm not instantiating an extra LABEL in the ITEM TEMPLATE.

The reason I'm not doing the extra label, is because we just don't need it in there. That's just a bit more processor/memory overhead that you just don't need to incur.

As for passing the unformatted value to the text field, that's because it'll ultimately be easier to validate without having to parse out the commas and other string elements.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...