Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can someone explain this function for two decimal places

Status
Not open for further replies.

robGtech

Programmer
Jul 19, 2006
4
US
Hello I found this function here yesterday and while it works great for values of only 1 decimal place, when I get a value with no decimal place it doesn't work. Can some one help me modify this.

I'm using Math.round and if I get 65, I need it to display 65.00

Code:
function numberFormat(str)
{
	str = str.toString();
    if(str.indexOf(".") == -1)
    {
        return str;
    }  
    splitString = str.split(".");
    
    var newSplitString = splitString[1];
    
    for(i=0; i < (2 - splitString[1].length); i++)
    {
        newSplitString = newSplitString + "0";
    }
    
    return (splitString[0] + "." + newSplitString);
}

Thanx
 
evidently someone was trying to reinvent the wheel.

take a look at this:

Code:
<script type="text/javascript"><!--

var v1 = 1234.567890;
var v2 = 1234.56;
var v3 = 1234.5;
var v4 = 1234;
var v5 = .12345;

document.writeln( v1 + " becomes " + v1.toFixed( 2 ) + "<br />" );
document.writeln( v2 + " becomes " + v2.toFixed( 2 ) + "<br />" );
document.writeln( v3 + " becomes " + v3.toFixed( 2 ) + "<br />" );
document.writeln( v4 + " becomes " + v4.toFixed( 2 ) + "<br />" );
document.writeln( v5 + " becomes " + v5.toFixed( 2 ) + "<br />" );

//--></script>



*cLFlaVA
----------------------------
[tt]( <P> <B>)13 * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
re-inventing the wheel indeed. What you wrote there works perfect.

Thank you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top