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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

applying 2 decimal function to a form value

Status
Not open for further replies.

photoxprt1868

Programmer
Jul 22, 2005
76
0
0
US
Hello,

I found this function here to always have two decimal places after rounding but how can I apply it to a form value

this is the function

Code:
<script language="javascript">
<!--
function numberFormat(str)
{
    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);
}
document.write(numberFormat('2.1'))
-->
</script>

and this is where I need to apply it

Code:
document.compareForm.charge2005.value = "$ " + cost2005;

or when I actually calculate it in the head

Code:
var fuel2006 = Math.round(((fuel1st1000kwh * first1000) + (fuelOver1000kwh * over1000))*100)/100;

Thank you very much for your help
 
I had tried that before and it gave me a js error.

"Object doesn't support this property or method"

I'm sure the error is how I'm passing the variable because if I try it with just a fixed number then it works

do you know where the error might be?

Thank you
 
got it. you're passing a numeric value into the numberFormat function. you can't perform string functions on a numeric value (split, indexOf, etc.).

I added one line to the very top of your numberFormat function and it all worked...

Code:
str = str.toString();



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

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top