Click here to Skip to main content
15,888,521 members
Articles / Web Development / HTML
Technical Blog

Get Currency Format Using Google in ASP.NET

Rate me:
Please Sign up or sign in to vote.
3.50/5 (2 votes)
16 Oct 2015CPOL2 min read 10.4K   1  
How to get currency format using Google in ASP.NET

Introduction

Many times, I have encountered a vital question, "How to change 20000.00 to $20,000.00". This is nothing but a simple currency formatter. You can use either jquery or server side coding to show the currency in this format. Here in this post, I will show you how to format the currency input from user in ASP.NET.

Format Currency using JQuery

First, start with JQuery. Google has provided a very simple way to format currency. You can find it out from here. Or you can follow this one.

To continue, you have to download two js files. One is jquery.formatCurrency-1.4.0.js and the second one is jquery.min.js. I have attached these js files with the code I have attached along with this post.

So let's start with sample coding. First, create a new project and add a new page, name it whatever you want. Then, add a new text box. Firstly, we will do it with onBlur function of jquery, so we don't need any more extra button for showing our formatted currency.

Add those downloaded js files into your header section of web form. And then paste the following code into your page.

HTML
<script src="jquery.min.js"></script>
<script src="jquery.formatCurrency-1.4.0.js"></script>
<script type="text/javascript">
        $(document).ready(function () {
            $('.text').focusout(function () {
                $('.text').formatCurrency();
                $('.text').formatCurrency('.currencyLabel');
            });
        });       

</script>

Now copy the text box.

ASP.NET
<div>
     <p>Currency Formatter</p>
     <asp:TextBox runat="server" 
     ID="txtPrice" CssClass="text"></asp:TextBox>
     Show by Jquery: <span class="currencyLabel"></span>

</div>

Check the CssClass of text box. It's the thing by which formatCurrency() method is calling to format it to text box and also show the output value to a span.

Format Currency using C#

I hope it's clear to you how to format currency by JQuery, now let's see how to do this using C#. Don't worry, C# has an inbuilt function for this. For C#, we are taking an extra button to display the output into a label.

ASP.NET
<asp:TextBox ID="txtCurrency" 
runat="server"></asp:TextBox>
<asp:Button ID="btnChange" Text="Format" 
runat="server" OnClick="btnChange_Click"    />
<asp:Label ID="lblShow" runat="server"></asp:Label>

C# Code

C#
protected void btnChange_Click(object sender, EventArgs e)
{
    lblShow.Text = (Convert.ToDouble(txtCurrency.Text)).ToString("C2");
}

Make sure this method is only applicable to data type like decimal and double. So you have to add a check whether user input is bound to numbers.

Download now

This article was originally posted at http://asp-arka.blogspot.com/feeds/posts/default

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer PwC
India India
I am a Software developer having an experience of 5 years in application development. To get me you can mail me at arkadeepde@gmail.com or you can visit my blog at ASP With Arka

Comments and Discussions

 
-- There are no messages in this forum --