ID:150152
 
Ok I need to work some numbers in my code to round off (.1 - .9) numbers. Is there a predefine proc for this?
Or do I need to make some code to round off the numbers myself?

LJR
round()
LordJR wrote:
Ok I need to work some numbers in my code to round off (.1 - .9) numbers. Is there a predefine proc for this?
Or do I need to make some code to round off the numbers myself?

To round to the nearest integer, use round(n,1). To round to the nearest X, use round(n,X). To cut off everything after the decimal point and just use the integer part of a number (not the same as rounding), use round(n) with no second argument.

Lummox JR
In response to Lummox JR
Thanks guys!! I figure there would be something to make it easier! ;)

YAY! Now I can make it so people only loose 1/3 of their gold!!! And not all of it!!!.. after loosing in battle.

LJR
In response to Evilkevkev
hmm I seem to be having problems with my code.

mob/pc
Login()
var/A, B
A = 9
B = A/2
round(B,1) (line 21)
usr << B

This gives me this on the compile

dm:21:round :warning: statement has no effect

What do I need to change, this is just a test setup to check the code before I enter it where it needs to go in my actual project.

LJR
In response to LordJR
LordJR wrote:
hmm I seem to be having problems with my code.

mob/pc
Login()
var/A, B
A = 9
B = A/2
round(B,1) (line 21)
usr << B

This gives me this on the compile

dm:21:round :warning: statement has no effect
I finally got it to work with this code if anyone ever has the same prob.

mob/pc
Login()
var/A, B
A = 9
A = A/2
B = round(A)
usr << B
In response to LordJR
LordJR wrote:
round(B,1) (line 21)
...
dm:21:round :warning: statement has no effect

For future reference, you get this error because DM knows round() is supposed to return a value, but you're not using the return value for anything. This statement doesn't change B; it merely calculates the round-off for you. What you need is this:
B = round(B,1)

Lummox JR
In response to LordJR
LordJR wrote:
I finally got it to work with this code if anyone ever has the same prob.

mob/pc
Login()
var/A, B
A = 9
A = A/2
B = round(A)
usr << B

This can be greatly abbreviated:
mob/pc
Login()
usr << round(9/2)

or if you need to keep the value of B:
mob/pc
var B
Login()
B = round(9/2)
usr << B