33 users online (0 members and 33 guests)  


  Results 1 to 7 of 7

Related

  1. Replies: 3
  2. Centering round a certain point    Forum: HTML Forum
    Replies: 2
  3. How to format Mb numbers?    Forum: CGI Perl Forum
    Replies: 1
  1. #1
    blink's Avatar
    New User

    Status
    Offline
    Join Date
    Mar 2005
    Posts
    19

    How to round numbers

    Hello,

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

  2. #2
    ALL's Avatar
    Super Dooper Nerd

    Status
    Offline
    Join Date
    Feb 2005
    Location
    localhost
    Posts
    382

    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. #3
    ALL's Avatar
    Super Dooper Nerd

    Status
    Offline
    Join Date
    Feb 2005
    Location
    localhost
    Posts
    382

    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. #4
    haunsa18's Avatar
    New User

    Status
    Offline
    Join Date
    Jul 2007
    Posts
    6

    Re: How to round numbers

    plz. specify more clearly.

  5. #5
    ALL's Avatar
    Super Dooper Nerd

    Status
    Offline
    Join Date
    Feb 2005
    Location
    localhost
    Posts
    382

    Re: How to round numbers

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

  6. #6
    pageBuzz.com's Avatar
    New User

    Status
    Offline
    Join Date
    Oct 2006
    Posts
    21

    Re: How to round numbers

    Quote Originally Posted by ALL View Post
    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.
    Last edited by pageBuzz.com; 08-01-2007 at 07:39 AM.

  7. #7
    xmassey's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Apr 2007
    Posts
    100

    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



Tags for this Thread