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

Parse float

Status
Not open for further replies.

glenmac

Technical User
Joined
Jul 3, 2002
Messages
947
Location
CA
Is there a funtion in javascript thats like vb script for changing a variable to currency? In VB script it's
Ccur(Variable) I think.
 
I don't think so....but we use this function to take a number & add on the cents if they're not there...
Code:
 function cent(amount) {
   // returns the amount in the .99 format
     amount -= 0;
     amount = (Math.round(amount*100))/100;
     return (amount == Math.floor(amount)) ? amount + '.00' :
            (  (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
 }
Hope this helps,
Jessica
 
Thanks for your help. Can I use this function to do calculations. Or should I just calculate first the n use it to format my answer? Maybe I shopuld just stay with VB.
 
You could go either way...but I'd probably convert the amounts first then total them. Like...
var price1 = document.forms[0].wt_price1.value;
var price2 = document.forms[0].wt_price2.value;
var total = cent(price1) + cent(price2); Hope this helps,
Jessica
[ponytails2]
 
Works great thanks a bunch. Guess maybe javascript isn't that bad.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top