Results 1 to 10 of 10

Thread: Equivalent of Format$ in Javascript?

  1. #1

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935

    Equivalent of Format$ in Javascript?

    I need a number to be in the form "######.##" (or at least two decimal places). Right now I'm using a convoluted system of ifs to do it, isn't there a better way?

  2. #2
    scoutt
    Guest
    here is a rounding 2 to decimal places. not sure if it will help, but might give you some ideas.

    EX: 7.9 equals 7.90, or 7.777 equals 7.78, and so on.
    Code:
    <html> 
    <head> 
    <title>Untitled</title> 
    <script language=javascript> 
    Function roundIt(amount) 
    { 
    var s = ""; 
    var Decimal; 
    amount = parseFloat(amount); 
    If (!(isNaN(amount))) { 
    // round to nearest cent 
    amount = Math.round(amount * 100); 
    amount = amount / 100; 
    
    // format the output 
    s = New String(amount); 
    Decimal = s.indexOf("."); 
    If (Decimal == -1) { 
    // whole number 
    s+= ".00"; 
    } Else { 
    If (Decimal == (s.length - 2)) { 
    // needs a trailing zero 
    s+= "0"; 
    } 
    } 
    } Else { 
    // not a number so return zero 
    s = "0.00"; 
    } 
    document.form1.Text1.value=s; 
    //calculate(); 
    Return s; 
    } 
    </script> 
    </head> 
    <body> 
    <form Name=form1> 
    <br><br> 
    <Input Type=Text Name=Text1 onBlur="roundIt(this.value)"> <Input Type=button value="Round It" onClick="roundIt(document.form1.text1.value)"> 
    <br> 
    </body> 
    </html>

  3. #3

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    Yeah, that's what I was doing (I am having it only go to one place because of how complex it starts to get). I was hoping that something like Math.round() could help.

  4. #4

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    So much for Math.round: http://home.netscape.com/eng/mozilla...ef_r-r.htm#308

    BTW, I forgot the most important thing: if it rounds to something like 1.10, 1.00, 3.20, 4.00, etc., I need the trailing zeros.

  5. #5

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    *ahem*

  6. #6
    chenko
    Guest
    Originally posted by filburt1
    So much for Math.round: http://home.netscape.com/eng/mozilla...ef_r-r.htm#308

    BTW, I forgot the most important thing: if it rounds to something like 1.10, 1.00, 3.20, 4.00, etc., I need the trailing zeros.
    dont you mean leading zeros

  7. #7

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    0001 = leading, 1.000 = trailing

  8. #8
    chenko
    Guest
    Originally posted by filburt1
    BTW, I forgot the most important thing: if it rounds to something like 1.10, 1.00, 3.20, 4.00, etc., I need the trailing zeros.
    "######.##"

    ummm, 1.10 and , 1.00 already have the trailing zeros, and you are saying that you need trailing? ummm, i would rather need leading zeros to get ######.##

  9. #9
    scoutt
    Guest
    here try this...

    Code:
    function formatNumber(expr, decimals) {
             var str = "" + Math.round( eval(expr) * Math.pow(10,decimals))
             while (str.length <= decimals) { str = "0" + str } // maybe add some leading zeros
             var decpoint = str.length - decimals // find location of decimal point
             var result = str.substring(0,decpoint);
             if (decimals) result += "." + str.substring(decpoint,str.length);
             return result;
    }
    eg. formatNumber(7.777,2) returns "7.78"
    formatNumber(1+1,2) returns "2.00"
    formatNumber(20/3,0) returns "7"
    At the moment it evaluates an expression as input, but doesn't check for rubbish input, but you could change that by doing:
    function formatNumber(num, decimals) {
    if (isNaN(parseFloat(num)) return "0";
    var str = "" + Math.round( parseFloat(num) * Math.pow(10,decimals))
    etc.

  10. #10

    Thread Starter
    Member filburt1's Avatar
    Join Date
    Aug 1999
    Posts
    6,935
    Thanks, I'll try it out.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width