View Full Version : Parsing numbers...

Tanker

07-27-2002, 08:36 AM

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.

Cloudski

07-27-2002, 11:45 AM

Try this:

<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 :thumbsup:

x_goose_x

07-27-2002, 05:22 PM

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.

Tanker

07-27-2002, 09:49 PM

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.

adios

07-29-2002, 12:12 AM

<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.

Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.