SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot akohl's Avatar
    Join Date
    Jun 2001
    Location
    Israel
    Posts
    184
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    formating numbers

    Is there an equivalent method in Javascript to the vb script function formatNumber()?

  2. #2
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try these, if thats not what your after let me know

    formatNumber(300, "$0.00")
    formatNumber(3.14159265, "##0.####")
    formatNumber(3.14, "0.0###%")
    formatNumber(314159, ",##0.####")
    formatNumber(31415962, "$,##0.00")
    formatNumber(cat43, "0.####%")
    formatNumber(0.5, "#.00##")
    formatNumber(0.5, "0.00##")
    formatNumber(0.5, "00.00##")
    formatNumber(4.44444, "0.00")
    formatNumber(5.55555, "0.00")
    formatNumber(9.99999, "0.00")

    have a look here

    http://www.4guysfromrolla.com/webtec...atNumber.shtml

  3. #3
    SitePoint Zealot akohl's Avatar
    Join Date
    Jun 2001
    Location
    Israel
    Posts
    184
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks,
    That's a cool one.

    What I was really asking was if Javascript has a built in function, ie part of the language, which does this.
    I guess it doesn't if someone had to write a funciton to do it.

  4. #4
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not quite sure what you meant, but..

    It is treated as a built in function by the looks of it.

    function formatNumber(obj_to_format, method)
    {
    //...
    }

    just use these

    formatNumber(300, "$0.00")
    formatNumber(3.14159265, "##0.####")
    formatNumber(3.14, "0.0###%")
    formatNumber(314159, ",##0.####")
    formatNumber(31415962, "$,##0.00")
    formatNumber(cat43, "0.####%")
    formatNumber(0.5, "#.00##")
    formatNumber(0.5, "0.00##")
    formatNumber(0.5, "00.00##")
    formatNumber(4.44444, "0.00")
    formatNumber(5.55555, "0.00")
    formatNumber(9.99999, "0.00")
    each one will display the number in a different format

    like this one will give you

    formatNumber(300, "$0.00")

    $300.00 as the output

    try them all out, or do you need something slightly different?

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by akohl

    What I was really asking was if Javascript has a built in function, ie part of the language, which does this.
    No there isn't as far as I know. I haven't heard of a built-in formatNumber function in JS, unless it's either an Internet Explorer extension (many VBScript functions were carried over to MS' interpretation of JavaScript, although this looks different to the VBScript formatNumber function) or a script that you're using.

    Actually though, that script you linked to doesn't look too bad...

  6. #6
    SitePoint Evangelist
    Join Date
    Jan 2002
    Posts
    420
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    im a little confused

    Andrew-J is that for JavaScript or VBScript ??
    Wavelan

  7. #7
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well LTW-Shark said it was carried over from vbscript. This was the javascript version, but i'm not sure what the formatting is for vbscript. Also vbscript will only work in internet explorer

  8. #8
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The 4guysfromrolla.com script has the same arguments as its VBScript (= ASP, FWIW ) equivalent (although the arguments seem switched around a bit).

    The format shown in the second and 4th posts is the one I was questioning... you seem to use it like the Visual Basic Format() function but I don't recall such a function in JavaScript - or VBScript for that matter.

    It's an argument not worth persuing I know, I'm just responding for the sake of completeness.

    Hope this helped!


Bookmarks

Posting Permissions

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