formatnumber(expression, numdigitsafterdecimal, includeleadingdigit,
useparensfornegativenumbers, groupdigits)
======================================================================
function formatnumber(num,decimalnum,bolleadingzero,bolparens,bolcommas)
/**********************************************************************
in:
num – the number to format
decimalnum – the number of decimal places to format the number to
bolleadingzero – true / false – display a leading zero for
numbers between -1 and 1
bolparens – true / false – use parenthesis around negative numbers
bolcommas – put commas as number separators.
retval:
the formatted number!
**********************************************************************/
{
if (isnan(parseint(num))) return "nan";
var tmpnum = num;
var isign = num < 0 ? -1 : 1; // get sign of number
// adjust number so only the specified number of numbers after
// the decimal point are shown.
tmpnum *= math.pow(10,decimalnum);
tmpnum = math.round(math.abs(tmpnum))
tmpnum /= math.pow(10,decimalnum);
tmpnum *= isign; // readjust for sign
// create a string object to do our formatting on
var tmpnumstr = new string(tmpnum);
// see if we need to strip out the leading zero or not.
if (!bolleadingzero && num < 1 && num > -1 && num != 0)
if (num > 0)
tmpnumstr = tmpnumstr.substring(1,tmpnumstr.length);
else
tmpnumstr = "-" + tmpnumstr.substring(2,tmpnumstr.length);
// see if we need to put in the commas
if (bolcommas && (num >= 1000 || num <= -1000)) {
var istart = tmpnumstr.indexof(".");
if (istart < 0)
istart = tmpnumstr.length;
istart -= 3;
while (istart >= 1) {
tmpnumstr = tmpnumstr.substring(0,istart) + "," + tmpnumstr.substring(istart,tmpnumstr.length)
istart -= 3;
}
}
// see if we need to use parenthesis
if (bolparens && num < 0)
tmpnumstr = "(" + tmpnumstr.substring(1,tmpnumstr.length) + ")";
return tmpnumstr; // return our formatted string!
}
formatpercent(expression, numdigitsafterdecimal, includeleadingdigit,
useparensfornegativenumbers, groupdigits)
– returns an expression formatted as a percentage (multiplied by 100)
with a trailing % character.
(note!! this function must have formatnumber
also provided.)
========================================================================
function formatpercent(num,decimalnum,bolleadingzero,bolparens,bolcommas)
/**********************************************************************
in:
num – the number to format
decimalnum – the number of decimal places to format the number to
bolleadingzero – true / false – display a leading zero for
numbers between -1 and 1
bolparens – true / false – use parenthesis around negative numbers
bolcommas – put commas as number separators.
retval:
the formatted number!
**********************************************************************/
{
var tmpstr = new string(formatnumber(num*100,decimalnum,bolleadingzero,bolparens,bolcommas));
if (tmpstr.indexof(")") != -1) {
// we know we have a negative number, so place % inside of )
tmpstr = tmpstr.substring(0,tmpstr.length – 1) + "%)";
return tmpstr;
}
else
return tmpstr + "%"; // return formatted string!
}
formatcurrency(expression, numdigitsafterdecimal, includeleadingdigit,
useparensfornegativenumbers, groupdigits)
(note!! this function must have formatnumber
also provided.)
========================================================================
function formatcurrency(num,decimalnum,bolleadingzero,bolparens,bolcommas)
/**********************************************************************
in:
num – the number to format
decimalnum – the number of decimal places to format the number to
bolleadingzero – true / false – display a leading zero for
numbers between -1 and 1
bolparens – true / false – use parenthesis around negative numbers
bolcommas – put commas as number separators.
retval:
the formatted number!
**********************************************************************/
{
var tmpstr = new string(formatnumber(num,decimalnum,bolleadingzero,bolparens,bolcommas));
if (tmpstr.indexof("(") != -1 || tmpstr.indexof("-") != -1) {
// we know we have a negative number, so place $ inside of ( / after –
if (tmpstr.charat(0) == "(")
tmpstr = "($" + tmpstr.substring(1,tmpstr.length);
else if (tmpstr.charat(0) == "-")
tmpstr = "-$" + tmpstr.substring(1,tmpstr.length);
return tmpstr;
}
else
return "$" + tmpstr; // return formatted string!
}
formatdatetime(datetime, formattype) : returns an expression formatted
as a date or time
========================================================================
function formatdatetime(datetime, formattype)
/*
fomattype takes the following values
1 – general date = friday, october 30, 1998
2 – typical date = 10/30/98
3 – standard time = 6:31 pm
4 – military time = 18:31
*/
{
var strdate = new string(datetime);
if (strdate.touppercase() == "now") {
var mydate = new date();
strdate = string(mydate);
} else {
var mydate = new date(datetime);
strdate = string(mydate);
}
// get the date variable parts
var day = new string(strdate.substring(0,3));
if (day == "sun") day = "sunday";
if (day == "mon") day = "monday";
if (day == "tue") day = "tuesday";
if (day == "wed") day = "wednesday";
if (day == "thu") day = "thursday";
if (day == "fri") day = "friday";
if (day == "sat") day = "saturday";
var month = new string(strdate.substring(4,7)), monthnumber = 0;
if (month == "jan") { month = "january"; monthnumber = 1; }
if (month == "feb") { month = "february"; monthnumber = 2; }
if (month == "mar") { month = "march"; monthnumber = 3; }
if (month == "apr") { month = "april"; monthnumber = 4; }
if (month == "may") { month = "may"; monthnumber = 5; }
if (month == "jun") { month = "june"; monthnumber = 6; }
if (month == "jul") { month = "july"; monthnumber = 7; }
if (month == "aug") { month = "august"; monthnumber = 8; }
if (month == "sep") { month = "september"; monthnumber = 9; }
if (month == "oct") { month = "october"; monthnumber = 10; }
if (month == "nov") { month = "november"; monthnumber = 11; }
if (month == "dec") { month = "december"; monthnumber = 12; }
var curpos = 11;
var monthday = new string(strdate.substring(8,10));
if (monthday.charat(1) == " ") {
monthday = "0" + monthday.charat(0);
curpos–;
}
var militarytime = new string(strdate.substring(curpos,curpos + 5));
var year = new string(strdate.substring(strdate.length – 4, strdate.length));
document.write(strdate + "");
// format type decision time!
if (formattype == 1)
strdate = day + ", " + month + " " + monthday + ", " + year;
else if (formattype == 2)
strdate = monthnumber + "/" + monthday + "/" + year.substring(2,4);
else if (formattype == 3) {
var ampm = militarytime.substring(0,2) >= 12 && militarytime.substring(0,2) != "24" ? " pm" : " am";
if (militarytime.substring(0,2) > 12)
strdate = (militarytime.substring(0,2) – 12) + ":" + militarytime.substring(3,militarytime.length) + ampm;
else {
if (militarytime.substring(0,2) < 10)
strdate = militarytime.substring(1,militarytime.length) + ampm;
else
strdate = militarytime + ampm;
}
}
else if (formattype == 4)
strdate = militarytime;
return strdate;
}
ltrim(string) : returns a copy of a string without leading spaces.
==================================================================
function ltrim(str)
/***
purpose: remove leading blanks from our string.
in: str – the string we want to ltrim
retval: an ltrimmed string!
***/
{
var whitespace = new string(" \t\n\r");
var s = new string(str);
if (whitespace.indexof(s.charat(0)) != -1) {
// we have a string with leading blank(s)…
var j=0, i = s.length;
// iterate from the far left of string until we
// dont have any more whitespace…
while (j < i && whitespace.indexof(s.charat(j)) != -1)
j++;
// get the substring from the first non-whitespace
// character to the end of the string…
s = s.substring(j, i);
}
return s;
}
rtrim(string) : returns a copy of a string without trailing spaces.
==================================================================
function rtrim(str)
/***
purpose: remove trailing blanks from our string.
in: str – the string we want to rtrim
retval: an rtrimmed string!
***/
{
// we dont want to trip just spaces, but also tabs,
// line feeds, etc. add anything else you want to
// "trim" here in whitespace
var whitespace = new string(" \t\n\r");
var s = new string(str);
if (whitespace.indexof(s.charat(s.length-1)) != -1) {
// we have a string with trailing blank(s)…
var i = s.length – 1; // get length of string
// iterate from the far right of string until we
// dont have any more whitespace…
while (i >= 0 && whitespace.indexof(s.charat(i)) != -1)
i–;
// get the substring from the front of the string to
// where the last non-whitespace character is…
s = s.substring(0, i+1);
}
return s;
}
trim(string) : returns a copy of a string without leading or
trailing spaces
=============================================================
function trim(str)
/***
purpose: remove trailing and leading blanks from our string.
in: str – the string we want to trim
retval: a trimmed string!
***/
{
return rtrim(ltrim(str));
}
len(string) : returns the number of characters in a string
===========================================================
function len(str)
/***
in: str – the string whose length we are interested in
retval: the number of characters in the string
***/
{ return string(str).length; }
left(string, length): returns a specified number of characters from the
left side of a string
========================================================================
function left(str, n)
/***
in: str – the string we are lefting
n – the number of characters we want to return
retval: n characters from the left side of the string
***/
{
if (n <= 0) // invalid bound, return blank string
return "";
else if (n > string(str).length) // invalid bound, return
return str; // entire string
else // valid bound, return appropriate substring
return string(str).substring(0,n);
}
right(string, length): returns a specified number of characters from the
right side of a string
========================================================================
function right(str, n)
/***
in: str – the string we are righting
n – the number of characters we want to return
retval: n characters from the right side of the string
***/
{
if (n <= 0) // invalid bound, return blank string
return "";
else if (n > string(str).length) // invalid bound, return
return str; // entire string
else { // valid bound, return appropriate substring
var ilen = string(str).length;
return string(str).substring(ilen, ilen – n);
}
}
mid(string, start, length): returns a specified number of characters from a
string
============================================================================
function mid(str, start, len)
/***
in: str – the string we are lefting
start – our strings starting position (0 based!!)
len – how many characters from start we want to get
retval: the substring from start to start+len
***/
{
// make sure start and len are within proper bounds
if (start < 0 || len < 0) return "";
var iend, ilen = string(str).length;
if (start + len > ilen)
iend = ilen;
else
iend = start + len;
return string(str).substring(start,iend);
}
// keep in mind that strings in javascript are zero-based, so if you ask
// for mid("hello",1,1), you will get "e", not "h". to get "h", you would
// simply type in mid("hello",0,1)
// you can alter the above function so that the string is one-based. just
// check to make sure start is not <= 0, alter the iend = start + len to
// iend = (start – 1) + len, and in your final return statement, just
// return …substring(start-1,iend)
instr(str, searchforstr) : returns the location a character (charsearchfor)
was found in the string str
========================================================================
// instr function written by: steve bamelis – steve.bamelis@pandora.be
function instr(strsearch, charsearchfor)
/*
instr(strsearch, charsearchfor) : returns the first location a substring (searchforstr)
was found in the string str. (if the character is not
found, -1 is returned.)
requires use of:
mid function
len function
*/
{
for (i=0; i < len(strsearch); i++)
{
if (charsearchfor == mid(strsearch, i, 1))
{
return i;
}
}
return -1;
}
