376 users online (0 members and 376 guests)

# Thread: How to round numbers

Results 1 to 7 of 7
1. ## How to round numbers

Hello,

How do I round numbers in perl to the next even 100 or 1000?

2. ## Re: How to round numbers

try somthing like this:
Code:
```sub RoundNum { #example use: RoundNum("197436", 4) (the first one is the number the second one is the postion to round, which should be 7
#you may need an offset of +/-1
if(substr(\$_[0], \$_[1]-(length \$_[0]), \$_[1]-(length \$_[0])) >= 5){
substr(\$_[0], 0, \$_[1]-(length \$_[0])) && substr(\$_[0], \$_[1]-(length \$_[0]), (length \$_[0]), '0');
}
}```
I cannot test my code, but if it doesnt work, either wait a few days for me to get my computer back, or maybe somone else here can help you

3. ## Re: How to round numbers

sorry, (if a moderator wants to just change previous code and delete this post would be fine) i forgot to put an else and return statment in there:
Code:
```sub RoundNum { #example use: RoundNum("197436", 4) (the first one is the number the second one is the postion to round, which should be 7
#you may need an offset of +/-1
if(substr(\$_[0], \$_[1]-(length \$_[0]), \$_[1]-(length \$_[0])) >= 5){
return 1+(substr(\$_[0], 0, \$_[1]-(length \$_[0])-1) && substr(\$_[0], \$_[1]-(length \$_[0])-1, (length \$_[0]), '0'));
}else{
return substr(\$_[0], 0, \$_[1]-(length \$_[0])-1) && substr(\$_[0], \$_[1]-(length \$_[0])-1, (length \$_[0]), '0');
}
}```

4. ## Re: How to round numbers

plz. specify more clearly.

5. ## Re: How to round numbers

what needs to be more clear? it's a function that rounds numbers. it even gives an example.

6. ## Re: How to round numbers

Originally Posted by ALL
what needs to be more clear? it's a function that rounds numbers. it even gives an example.
I think the point was that they did not understand code at all. It gets frustrating to give great examples and get a question mark as a response. Its a nice sub.

7. ## Re: How to round numbers

Not sure at all what you mean by round 100 to 1000, but i'm sure you coould do it in one line of code...
Provide me with a couple of examples i.e
EXAMPLES:
I want to round the number 4.576432 to 2 decimal places (so 4.58).
Then I will help, but I have no idea what you mean by next even 100 or 1000