PDA

View Full Version : Showing Money with two decimal places


Steven_Smith
04-14-2003, 05:34 AM
Hi,

I have a form that does some adding of number for money. My problem is that when the string adds up to "17.50" it changes to "17.5"

How can I show two decimail places?

Thanks
Steve

Steven_Smith
04-14-2003, 06:06 AM
Here is what I did.


The var total would only be an even dollor, like 1 or it would be like 4.5

var number=total.toString(); --conevt to a string
if (totaler.indexOf(".5")!=-1) -- does it have .5 ???
var finalvalue="$" + totaler + "0" --Yes it does,
else
var finalvalue="$" + totaler + ".00" --No it doesn't
}

It works for me!


:thumbsup:

beetle
04-14-2003, 07:30 AM
And old method I c&p'd here for youString.prototype.currency = function( grouping, dollar, round )
{
var point = this.indexOf( "." );
var anynum = parseFloat( this );
var result = ( round ) ? Math.round( anynum * 100 ) / 100 : Math.floor( anynum * 100 ) / 100;
result = result.toString(10);

if (point == -1)
result = parseInt(anynum) + ".00";
else
{
var decimals = result.substring( point + 1 );
if ( decimals.length == 1 ) result = result + "0";
}

if ( grouping )
{
point = result.indexOf( "." );
var dollars = result.substring( 0, point );
var cents = result.substring( point );
var commas = parseInt( dollars.length / 3 );
var temp = new Array();
for ( var i = commas; i >= 0; i-- )
{
if ( dollars.length > 3 )
{
temp[i] = dollars.substring( dollars.length-3 );
dollars = dollars.substring( 0, dollars.length-3 );
}
else
{
temp[i] = dollars;
}
}
dollars = temp.join( "," );
result = dollars + cents;
}
return ( dollar ) ? "$ " + result : result;
}