Tad,
The ECMA 3.0 standard defines a function called toPrecision, as a
method of the Number object, that does what you need. I know that
Netscape Navigator 6 already supports it. I have read that Internet
Expolorer 5.5 for Windows also supports it, but I don't have it
available to test. I know that Internet Explorer 5 for MACs does NOT
support it.
Until the new standard is more widely implemented, you can make due
by following the example below. (Sorry I can't include the code as
attached files. This list does not permit that.) The file Number.js
is provided courtesy of www.wrox.com.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
<HTML>
<!-- Demo.html JavaScript Demo of ToPrecision formatting of
floating point numbers -->
<HEAD>
<TITLE>Demo of ToPrecision</TITLE>
<SCRIPT SRC="Number.js"></SCRIPT>
<SCRIPT TYPE="text/javascript">
Number.prototype.toFixed=Number_toFixed;
Number.prototype.toExponential=Number_toExponential;
Number.prototype.toPrecision=Number_toPrecision;
</SCRIPT>
</HEAD>
<BODY BGCOLOR="WHITE">
<SCRIPT TYPE = "text/javascript">
var num1 = 3.3333;
var num2 = 5.0;
var num3 = 6.6666;
var s1 = num1.toPrecision(3);
var s2 = num2.toPrecision(3);
var s3 = num3.toPrecision(3);
alert (num1 + " to two decimal places is " + s1 + "\n" +
num2 + " to two decimal places is " + s2 + "\n" +
num3 + " to two decimal places is " + s3);
</SCRIPT>
</BODY>
</HTML>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Number.js
// A couple of very simple examples...
// Number_isOdd()
function Number_isOdd()
{
return (this%2==0)?false:true;
}
// Number_isEven()
function Number_isEven()
{
return (this%2==0)?true:false;
}
// Number_toLocaleString()
if(!Number.prototype.toLocaleString)
Number.prototype.toLocaleString=Number.prototype.toString;
// Number_toFixed()
function Number_toFixed(fractionDigits)
{
var f,x,a,b,m,n,i,s,k,count,digit;
f=parseInt(fractionDigits);
x=Number(this);
s=z=i=m="";
n=1;
if(isNaN(f))
f=0;
if(f<0||f>20)
{
if(window.Error)
{
var err=new Error("The number of fractional digits is out of range.");
err.name="RangeError";
throw err;
}
else return 1/0; //forces an error state
}
if(isNaN(x))
return "NaN";
if(x<0)
s="-",x*=-1;
if(x>=Math.pow(10,21))
return s+x.toString();
n=Math.floor(x*Math.pow(10,f));
n+=Math.round(Math.abs(x*Math.pow(10,f)-n));
k=Math.floor(Math.log(n)/Math.LN10);
while(k>-1)
{
digit=Math.floor(n/Math.pow(10,k));
m+=digit;
n-=digit*Math.pow(10,k);
k--;
}
if(f==0)
return s+m;
k=m.length;
if(k<=f)
{
count=f+1-k;
while(count>0)
count--,z+="0"; // both expressions are evaluated;
// the value in the second one is returned
m=z+m;
k=f+1
}
return s+m.substring(0,k-f)+"."+m.substring(k-f);
}
// Number_toExponential()
function Number_toExponential(fractionDigits)
{
var f,x,m,k,s,err;
f=parseInt(fractionDigits);
x=Number(this);
s="";
if( isNaN(x) )
return "NaN";
if(x<0)
s = "-", x *= -1;
if( x == Infinity ) return s + "Infinity";
var removeTrailingZeros = isNaN(f);
if(removeTrailingZeros)
f=15;
if(f<0||f>20)
{
if(window.Error)
{
var err = new Error( "The number of fractional digits is out of
range." );
err.name = "RangeError";
throw err;
}
else
return 1/0; //forces an error state
}
k = Math.floor(Math.log( x )/Math.LN10);
m = (x / Math.pow(10, k)).toFixed(f);
if(removeTrailingZeros)
while(m.charAt( m.length-1) == "0" )
m = m.substring( 0, m.length-1 );
if( k>=0)
k = "+" + k;
return s + m + "e" + k;
}
// Number_toPrecision()
function Number_toPrecision(precision)
{
var f,x,m,k,s,err;
x = Number(this);
p = parseInt(precision);
if(isNaN(p))
return this.toString();
if(isNaN(x))
return "NaN";
s = "";
if(x<0)
s="-",x*=-1;
if(x ==Infinity )
return s+"Infinity";
if(p<1||p>21)
{
if(window.Error)
{
var err=new Error("The number of precision digits is out of range.");
err.name = "RangeError";
throw err;
}
else
return 1/0; //forces an error state
}
k = Math.floor(Math.log(x)/Math.LN10);
if(k<-6||k>=p)
return this.toExponential(p-1);
return this.toFixed(p-k-1);
}
Note that the "precision" argument to toPrecision counts the decimal
point. So if you want a decimal point followed by two digits, you
have to specify a precision of 3.
Hope this meets your needs.
--Greg
>Javascript has no facility for specifying the number of decimal places, so I
>wrote my own script for reformatting to 2dp. I'd post it, but I can't
>remember where it is:(
>
>This is the gist of it though:
>
>1.Locate the position of the decimal point (if any).
>2.Strip out the integer part and the decimal places part (if any).
>3. Append "0" to the decimal places part until it is length 2.
>4. Put the bits back together, sticking a decimal point in the middle.
>
>HTH
>Pat
>
>
>
>----- Original Message -----
>From: "Tad Chrisco" <tad1961@y...>
>To: "javascript" <javascript@p...>
>Sent: Saturday, December 15, 2001 6:18 AM
>Subject: [javascript] Formatting to 2 decimal places
>
>
>> I posted this question on beginning JavaScript but didn't receive an
>> answer. Can anyone here help? Thanks,
>>
>> Hi All,
>> I'm new to this group, but if it is anything like the vb and vi groups
>> then I'm sure it is very helpful. I have a little code here that when the
>> user enters into the first 2 textboxes quantities the other 3 textboxes do
>> calculations. The problem that I am having is that when one of the 3
>> latter textboxes ends with .50 the textbox only prints .5. Is there
>> something I can do to format to 2 decimal places to get that 0 in the
>> textbox. See code below. Thanks, Tad.
>>
>> <html><head><title>Parts</title>
>> <script language="JavaScript">
>> <!--
>> function CalculateTotals(){
>> f=document.orderform;
>>
>> f.grandtotal.value=(parseFloat(f.qty1.value)*7.50)
>> +(parseFloat(f.qty2.value)*14.00)
>>
>>
>> if (f.grandtotal.value <= 30)
>> f.sub.value = 4.95;
>> else if (f.grandtotal.value > 30 && f.grandtotal.value <=60)
>> f.sub.value = 5.95;
>> else if (f.grandtotal.value > 60 && f.grandtotal.value <=75)
>> f.sub.value = 6.95;
>> else f.sub.value = 7.95;
>>
>> f.finalTotal.value=parseFloat(f.grandtotal.value)
>> +parseFloat(f.sub.value);}
>> //-->
>> </script></head>
>> <body>
>> <form name="orderform" method="post" action="mailto:tad1961@h...">
>> <input name="qty1" size="3" OnBlur="CalculateTotals()" />
>> <input name="qty2" size="3" OnBlur="CalculateTotals()" />
>> <input name="grandtotal" size="7"
>> OnFocus="document.orderform.qty1.select();
>> document.orderform.qty1.focus();" />
>> <input name="sub" size="7"
>> OnFocus="document.orderform.qty1.select();
>> document.orderform.qty1.focus();" />
>> <input name="finalTotal" size="7"
>> OnFocus="document.orderform.qty1.select();
>> document.orderform.qty1.focus();" />
>> <br /><input type="submit" value="Make Purchase" />
>> </form>
>> <script language="JavaScript">
>> <!--
>> f=document.orderform;
>> f.qty1.value=0; f.qty2.value=0; f.grandtotal.value=0; f.sub.value=0;
>> //-->
>> </script>
>> </body></html>
>>
>>
>
>
>