Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: format number

  1. #1
    Senior Coder
    Join Date
    May 2004
    Thanked 0 Times in 0 Posts

    format number

    I have the following function

    function custRound(x,places) {
    return (Math.round(x*Math.pow(10,places)))/Math.pow(10,places)

    Can someone help edit this that it should always return a number with 2 decimal points. (.3 should become .30) and if possible put the , in where it belongs.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    London, England
    Thanked 2,573 Times in 2,551 Posts
    This is another topic which has been dealt with many times before and would have been answered by the Search tool.

    <script type="text/javascript">
    function formatNumber(x){
    return x.toFixed(2).toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1,').split('').reverse().join('').replace(/^[\,]/,'');


    Another easy way is to use

  • #3
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    72° W. 48' 57" , 41° N. 32' 04"
    Thanked 1 Time in 1 Post
    i wasn't sure if you wanted code, or help, so i figured i'd give help first, and then give code if you asked for it.
    here is a link to a contest we had a long while back about formatting numbers to look like currency. i liked a lot of the other entries better than mine, though my entry is there too.

    the first step to modifying code is to understand what the code you already have does. so, because programming languages owe almost everything to math, we're gonna follow some math rules, and work from the inside of the parentheses out. the farthest in you get is to use the Math.pow() method to raise 10 to the power of places, which you want always to be 2, so that's 10 squared, or 100. so you can replace all of that Math.pow() nonsense with 100. so you end up with (Math.round(x*100))/100 . which is a lot simpler to understand, i think.

    you want to make sure to do your rounding before you try to group your number with the comma, because the rounding method will negate all the work you do with grouping and padding. that probably doesn't make a whole lot of sense now, but if you ever pad and group first, and then round, then you'll see what i'm talking about. (you might want to try padding around grouping first, just for the hell of it, so you can see what i mean).

    grouping is pretty easy: you convert your number to a string, and then count from the end of the string, to the beginning. every 3rd place, you put in a comma. try taking a look at code to make a string backward; that should help.

    to pad a number with 0s, you compare its length (again, it has be converted to a string) with the number of decimal places you want. just run through a for-loop until your number's length is the same as the number of 0s you want.

    hope that's been helpful.
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.


    Posting Permissions

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