Solved

formatting text in literal control

Posted on 2004-10-12
3
1,140 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

820 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