Wife/Girlfriend/Sister?
Pepper Spray &
Stun Gun Specials!
KEEP THEM SAFE.
 CoverYourASP --> "Formatting.asp" --> Source

Free membership

Join in the fun! Sign in
Member Services

Site navigation
Download the entire site!
Search my articles
Free Magazines
Browse the directory

Send me feedback
Buy my boxer shorts

Recommend this page
Printer-friendly page

Resources I recommend
Link to my site
Advertising slashed!
About your privacy
Legal stuff
Site statistics
47 active users
455 visitors today
586 pages today
(only part of today)
Tools I use

CoverYourASP
Copyright © 1999-2010 James Shaw.
All rights reserved.

ASP.NET Blog
RSS submissions
E-commerce

Now open source with SourceForge!

This page shows the actual source code used on this site. You can read the article that discusses this code here.

If this is the first CYA source code you've seen you should read this overview first.

Did you know you can download all the source code (and the database) of this site? Then get my newsletter to be emailed when I update the source code!

Please spread the word by recommending my site to your friends and colleagues!

This is JScript (server-side JavaScript), not the more common VBScript. More...

Formatting.asp

<!--#include file = "include/Startup.asp"-->

<%
// ============================================
// NOTE: all source code downloaded from CoverYourASP was written by
// James Shaw (unless stated otherwise), and is copyright (c) 2000-2002
// by James Shaw. You can use the code for any purpose, but do not
// publish or distribute the content in any way.
//
// See http://CoverYourASP.com/Legal.asp for up-to-date details.
// ============================================

// output relevant meta tags
Init( "Format library" );

// output common top of page
Header( 'Format library' );

// output page content
Content ( );

// output common bottom of page
Footer( );

// ============================================
// the content of this page - every page has a function 'Content' that
// is called above.
// ============================================
function Content ( )
{
   Out ( '<td valign="top" class="content">' );

      Out ( 'I had to display some floating point numbers to 2 decimal places yesterday. (VBScript guys, please cover your eyes and ears, I\'m gonna whisper this)' );

      Out ( '<p><center><i>JavaScript doesn\'t provide a method for this</i></center>' );

      Out ( '<p>I couldn\'t believe it either. But being a die-hard C programmer I like challenges! I only had to deal with positive numbers in the application I was writing, so take heed. Negative numbers are not supported!' );

      Out ( '<p>Here are some examples of numbers, both raw and formatted to demonstrate. Note how they all have 2 decimal places and are rounded up to the nearest penny.' );

      Out ( '<center><table cellspacing="10" border="0">' );
         Out ( '<tr align="right"><td>Raw<br><hr></td><td>Formatted<br><hr></td></tr>' );

         var fData = new Array ( 1.0, 1.1, 1.23, 1.234567, 1.235, 1.236 );

         for ( var i=0; i<fData.length; i++)
            Out ( '<tr align="right"><td>' + fData [ i ] + '</td><td>' + FormatCurrency ( fData [ i ] )+ '</td></tr>' );

      Out ( '</table></center>' );

      Out ( '<p><center><a href="ShowSource.asp?page=Formatting"><img src="images/source.gif" border=0></a></center>' );

      ShowBottomBanner()

   Out ( '</td>' );
   Out ( '<td background="images/gx/navgap.gif" valign="top">' );

      // show rotating banners
      ShowBanners ( 2 );

   Out ( '</td>' );
}
%>

utils/Format.asp

<%
// ============================================
// NOTE: all source code downloaded from CoverYourASP was written by
// James Shaw (unless stated otherwise), and is copyright (c) 2000-2002
// by James Shaw. You can use the code for any purpose, but do not
// publish or distribute the content in any way.
//
// See http://CoverYourASP.com/Legal.asp for up-to-date details.
// ============================================

// ============================================
// make currency format
// ============================================
function FormatCurrency ( fPrice )
{
   // convert to string, and round up 2 dp
   var sCurrency = "" + ( fPrice + 0.00500000001 );

   // find .
   var nPos = sCurrency.indexOf ( '.' );

   if ( nPos < 0 )
   {
      sCurrency += '.00';
   }
   else
   {
      // hack off 3rd dp
      sCurrency = sCurrency.slice ( 0, nPos + 3 );

      // add up to 2 trailing zeroes if necessary
      var nZero = 3 - ( sCurrency.length - nPos );

      for ( var i=0; i<nZero; i++ )
         sCurrency += '0';
   }

   return sCurrency;
}

// ============================================
// get a simple date in "12/12/2000" format
// ============================================
function FormatDateDMY ( dateSeed )
{
   var date = new Date ( dateSeed );

   return (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear();
}


// ============================================
// get a verbose date in "Dec 12 2001" format
// ============================================
function FormatDateDMYv ( dateSeed )
{
   var date = new Date ( dateSeed );

   return sMonths [ date.getMonth() ] + ' ' + date.getDate() + ' ' + date.getFullYear ( );
}

// ============================================
// get a short date in "12 Dec" format
// ============================================
var sMonths = new Array ( "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" );

function FormatDateDM ( dateSeed )
{
   var date = new Date ( dateSeed );

   return date.getDate() + ' ' + sMonths [ date.getMonth() ];
}
%>

Hopefully much of this is self-explanatory. If not, or if you see ways that I can improve the code, please drop me a line.

To see the source code for this page, click on the icon below.

Featured sponsor
My favorite resources

Tiki Statues - Tiki Masks - Tiki Totems



Qualify for Free Trade Magazines

Free subscriptions to industry leading publications for those who qualify!

CoverYourASP Mugs, T-shirts, caps - even Boxer shorts...
I don't make a penny from these, but they're a lot of fun! Don't you need a new mouse mat?


I share my content

Supporting ASPRSS

Do you need a quick and easy way to link to my articles? All the information you need is published with ASPRSS...


New Proposal Kit Professional 5.1
Brand yourself as a top professional: create quotes and amazing proposals and get many legal documents free!

The latter saved me 3 times the purchase price on the first day I owned it!


See my source code
wherever you see this icon...

You can also download the entire site source code for FREE!