Solved

formatting text in literal control

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

617 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