Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jun 2002
    Location
    Northern California
    Posts
    50
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Parsing numbers...

    Its late and i'm sure im not thinking streight but what I want to do is take a number like 32658 and break it out to its 10's, 100's, 1000's values. So we would end up with the following numbers

    32
    6
    5
    8

    Thanks in advance.

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    U.S. (Wish Japan though)
    Posts
    141
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try this:

    Code:
    <script>
    var number = 32658
    thsnds = number/1000
    number = number - (thsnds * 1000)
    hundreds = number/100
    number = number - (hundreds * 100)
    tens = number/10
    number = number - (tens * 10)
    </script>
    I think you can modify it for your needs from here....

    Hope that helps

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Montreal, Canada
    Posts
    644
    Thanks
    0
    Thanked 0 Times in 0 Posts
    for the above script to work, you need to floor each result:

    var number = 32658
    thsnds = Math.floor(number/1000)
    number = number - (thsnds * 1000)
    hundreds = Math.floor(number/100)
    number = number - (hundreds * 100)
    tens = Math.floor(number/10)
    number = number - (tens * 10)
    document.write(thsnds+" - "+hundreds+" - "+tens+" - "+number)

    But what I suggest is something a little more expandable:

    var number = "32658";
    digits = new Array();
    for (x=0; x<number.length; x++) {
    digits[x] = number.charAt(x);
    }
    alert(digits);

    The only problem is that "32" (thousand) is considered "3" and "2", but I don't see the point to keeping them together.

  • #4
    New Coder
    Join Date
    Jun 2002
    Location
    Northern California
    Posts
    50
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Ok, I guess a little reasoning behind the code...

    I play everquest, in the game you can become a trader, it creates a file of the items you have with the price that you want for it in copper pieces (dnd folks should remember this). I need to conver the number of copper pieces in this list to their respective platinum / gold / silver / copper (if any copper left over). It breaks down to
    10 copper = 1 silver
    10 silver = 1 gold
    10 gold = 1 platinum

    so 32658 copper would work out to 32pp, 6gp, 5sp, 8cp

    hopefully this helps. Thanks again for all the suggestions.

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    <html>
    <head>
    <title>untitled</title>
    <script type="text/javascript" language="JavaScript">

    function cash_in(copper) {
    var tempObj = new Object();
    tempObj.pp = parseInt(copper/1000);
    tempObj.gp = parseInt(copper%1000/100);
    tempObj.sp = parseInt(((copper%1000)%100)/10);
    tempObj.cp = (((copper%1000)%100)%10);
    tempObj.print = function() {
    return 'PLATINUM: ' + this.pp + 'pp\nGOLD: ' + this.gp + 'gp\n' +
    'SILVER: ' + this.sp + 'sp\nCOPPER: ' + this.cp + 'cp\n';
    }
    return tempObj;
    }

    function demo() {
    var c = prompt('Enter number of copper pieces:','cp');
    c = c.replace(/,/g, '');
    if (!isNaN(Number(c))) {
    c = cash_in(Number(c));
    alert(c.print());
    }
    }

    </script>
    </head>
    <body>
    <form><input type="button" value="Cash In" onclick="demo()"></form>
    </body>
    </html>

    Gives you a nice little 'handable object' - with an outputting method, if you need it.


  •  

    Posting Permissions

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