109 users online (0 members and 109 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?  Reply With Quote

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(\$_, \$_-(length \$_), \$_-(length \$_)) >= 5){
substr(\$_, 0, \$_-(length \$_)) && substr(\$_, \$_-(length \$_), (length \$_), '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  Reply With Quote

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(\$_, \$_-(length \$_), \$_-(length \$_)) >= 5){
return 1+(substr(\$_, 0, \$_-(length \$_)-1) && substr(\$_, \$_-(length \$_)-1, (length \$_), '0'));
}else{
return substr(\$_, 0, \$_-(length \$_)-1) && substr(\$_, \$_-(length \$_)-1, (length \$_), '0');
}
}```  Reply With Quote

4. ## Re: How to round numbers

plz. specify more clearly.  Reply With Quote

5. ## Re: How to round numbers

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

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.  Reply With Quote

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  Reply With Quote

numbers, round 