Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

formatting text in literal control

Posted on 2004-10-12
3
Medium Priority
?
1,154 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 750 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
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.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

722 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