Solved

formatting text in literal control

Posted on 2004-10-12
3
1,129 Views
Last Modified: 2012-06-27
Hi ,

I'm using a literal controls in table cell for displaying data.
 I need to format the  data in the following formats

$99,999.99
 $99.99
$9.99

How to do this

thanks
Anitha
0
Comment
Question by:AnithaB
3 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 12290106
<asp:Literal id="Literal1" runat="server"></asp:Literal>

use javascript:
<scrip language="javascript">
<!--
function formatCurrency(num) {
                  d=new Date()
                  if (d.getTimezoneOffset( ) > 60) currSign='$'
                  else currSign='£'

                  num = num.toString().replace(/\$|\,/g,'');
                  if(isNaN(num))
                        num = "0";
                  sign = (num == (num = Math.abs(num)));
                  num = Math.floor(num*100+0.50000000001);
                  cents = num%100;
                  num = Math.floor(num/100).toString();
                  if(cents<10)
                        cents = "0" + cents;
                  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
                        num = num.substring(0,num.length-(4*i+3))+','+
                              num.substring(num.length-(4*i+3));
                  return (((sign)?'':'-') + currSign + num + '.' + cents);
            }
//-->
</script>

Then inside your Page_Load put the following code:

Literal1.Attributes.Add("onload","javascript:formatCurrency(this.value);")
0
 
LVL 2

Accepted Solution

by:
raindog_mx earned 250 total points
ID: 12294301
Hi,

The easiest way to do it would be to use String's Format method. This is a working example from the server side.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim value As Integer = 10003 'This would be the value you want to display

        Literal1.Text = String.Format("{0:c}", value) 'More details about this following

End Sub

The result would be a literal displaying

$10,003.00

The String.Format method thakes two arguments by default: the first is the format string, and the second is the object wich will be formated, it can be anything such as the integer in the example or an array.

The format string is written in the following form "{0:c}" where 0 means the index of the value you'll format from the object you're passing, it's commonly 0 as you'll be passing a single value hence it's array index is zero. The ":c" part is where you specify you want the object formatted as currency, what the server will do is check for its country code and will format according to it's country currency standards.

There are more format strings, like d, "{0:d}" would display the value as a date.

At last, you can get the same result with this other form:

   Literal1.Text = value.toString("C") '<--- this will automatically format the value as a currency string

more information here: http://www.dotnetspider.com/Technology/KBPages/241.aspx
and here: http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=181



0
 

Author Comment

by:AnithaB
ID: 12298020
Thanks for your simple solution

Anitha
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Accessibility and Usability are two concepts that seem to be closely related.  But, too many people seem to have a distorted perception of them. During last five years, those two words have come to the day-to-day work of almost every web develope…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now