in reply to Rounding to the nearest 50

Considering that sprintf knows how to do rounding, it's just a trivial matter of scaling:
#!/usr/bin/perl use strict; use warnings; foreach my $n (0, 1, 10, 24, 25, 26, 49, 50, 51, 74, 75, 76) { printf "%3d: %3d\n", $n, 50 * sprintf "%.0f" => $n / 50; } __END__ 0: 0 1: 0 10: 0 24: 0 25: 0 26: 50 49: 50 50: 50 51: 50 74: 50 75: 100 76: 100
Note the bankers rounding.

Abigail

Replies are listed 'Best First'.
Re: Re: Rounding to the nearest 50
by Crian (Curate) on Mar 01, 2004 at 15:19 UTC
    but that is not, what the OP wanted (151->200)
      You're right. He must have another definition of 'nearest' than I have.

      Abigail

        Thank you all very much. I am dealing with currency (0 - 10000000). I am sure something in this post will set me straight. Thank you all again very much.
        Yes, I agree. I wrote him in my answer above, that he is not looking for the nearest value (or that, what I would tell the nearest).
        Perhaps he really meant "nearest 100" but jumbled that with the notion that anything within 50 would round to it.

        The PerlMonk tr/// Advocate