ToFmt
JavaScript object described here and its associated
methods enable numbers to be output as floating point values or powers
of 10 with a specified width and number of figures after the decimal point,
integer values, or to attach a leading zero to single digit numbers. Brief
descriptions of the methods, examples of their use, and a series of test
examples to check that the code works correctly in your browser are given
below. The object and methods are included in the <HEAD>
section of this document which you can access if you choose to view
the document source.
Please let me know if you find these objects useful, or contact me with
any comments you may have.
ToFmt
objectvar a = new ToFmt(x);where
x
is a numerical variable or number.
fmt00()
Date
object.
fmtF(w,d)
:
Fw.d
, where w
is the
width of the field and d
is the number of figures after
the decimal
point. The result is rounded either up or down to be correct to the
number of digits requested.
The result is aligned to the right of the field. The default
padding character is a space " "
. This can be modified using the
setSpacer(string)
method of ToFmt
.
If the result will not fit in the field , the field will be returned
containing w
asterisks.
fmtE(w,d)
Ew.d
, where w
is the
width of the field and d
is the number of figures after the decimal
point. The result is rounded either up or down to be correct to the
number of digits requested.
The result is aligned to the right of the field. The default
padding character is a space " "
. This can be modified using the
setSpacer(string)
method of ToFmt
.
If the result will not fit in the field , the field will be returned
containing w
asterisks.
fmtI(w)
Iw
, where w
is the
width of the field.
Floating point values are truncated (rounded down) for integer
representation.
The result is aligned to the right of the field. The default
padding character is a space " "
. This can be modified using the
setSpacer(string)
method of ToFmt.
If the result will not fit in the field , the field will be returned
containing w
asterisks.
setSpacer(string)
" "
as the spacer character in your HTML output. This
will not allow for column alignment unless you use a fixed width font, but
will otherwise let your numbers breathe a bit.
fmt00
<SCRIPT LANGUAGE="JavaScript"> <!-- function tst00(){ var a = new ToFmt(document.fm1.fta1.value); document.fm1.fta2.value=a.fmt00(); } // --> </SCRIPT>and the HTML for the form is:
<FORM NAME="fm1"> Enter a postive integer: <INPUT TYPE="TEXT" SIZE=3 NAME="fta1"> <INPUT TYPE="BUTTON" VALUE="Format!" onClick="tst00();return true;"> Result: <INPUT TYPE="TEXT" SIZE=3 NAME="fta2"> </FORM>The function first creates a new
ToFmt
object with
the contents of the first text field of the form (named fta1
),
and sets the value of the second text field to the result of the
fmt00()
method invoked on the ToFmt
object
created.
fmtF(w,d),fmtE(w,d),fmtI(w)
fmtF(w,d)
,fmtE(w,d)
,
and fmtI(w)
functions. Introduce a positive or negative
number in the first text field.
The JavaScript code used here is:
<SCRIPT LANGUAGE="JavaScript"> <!-- function tst01(){ var a = new ToFmt(document.fm2.fta1.value); document.fm2.fta2.value=a.fmtF(13,5); document.fm2.fta3.value=a.fmtE(13,6); document.fm2.fta4.value=a.fmtI(6); } // --> </SCRIPT>where the function
tst01
is invoked from
the form named fm2
, with text areas fta1
-
fta4
.
December 1999. [email protected]