Click to See Complete Forum and Search --> : I need a script involving numbers.
lonemalika
06-05-2003, 05:04 AM
I have variables that are numbers. As a matter of fact, it's a dice rolling script, but after the "battle", you can calculate your EXP and gold gotten. I want to have the gold come up as either 1/2 of the enemy's HP or 3/4 of it, but I want to alleviate any chance of decimals. In other words, I need a script that can change *ANY* decimal into the closest integer(using our 1-4, 5-9 rounding system). Thanks in advance, and feel free to e-mail me(spork_kitty@yahoo.com)
Charles
06-05-2003, 05:16 AM
Math.round()
brendandonhue
06-05-2003, 05:17 AM
If your gold is 1/2 of the enemy's HP, and their HP is in a variable called HP, this is how you would do it
<script language="javascript">
var HP = 5;
var gold = .5*HP;
var gold = parseInt(gold);
alert(gold)
</script>
See, parseInt converts to integer, parseFloat to floating point.
Charles
06-05-2003, 05:41 AM
The function parseInt() takes as its first parameter a string (http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/toplev.html#1064173). With brendandonhue's example above, the variable gold is a number primitive. When passed to parseInt() it is first converted into a Number wrapper object and Number.toString() is called. Using Math.round() (http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/math.html#1197725) is a much more direct method.
brendandonhue
06-05-2003, 05:56 AM
Yeah, I knew that :D
lonemalika
06-06-2003, 01:11 AM
Originally posted by Charles
The function parseInt() takes as its first parameter a string (http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/toplev.html#1064173). With brendandonhue's example above, the variable gold is a number primitive. When passed to parseInt() it is first converted into a Number wrapper object and Number.toString() is called. Using Math.round() (http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/math.html#1197725) is a much more direct method.
How would I Math.round()?
function Math.round(gold)?
or
[this is what my function stuff is]
function calc2()
{
--calculations for EXP and gold
Math.round(gold)
}
??? I'm a newbie you could say. The javascript I know, I've learned as I went along.
Charles
06-06-2003, 05:04 AM
alert(Math.round(Math.PI))