Solved

formatting text in literal control

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# GridRow get Old/New Value 1 62
Stupid git question 2 82
text on slider doesn't resize nicely 5 56
Scroll 5 news at a time. 4 25
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
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.
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:
This video teaches users how to migrate an existing Wordpress website to a new domain.

813 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

13 Experts available now in Live!

Get 1:1 Help Now