Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How can i cut donw a number (ex. 10.8766756) to 10.8 ??

Replies are listed 'Best First'.
Methods to format a number
by Corion (Patriarch) on Jun 02, 2000 at 18:29 UTC

    There are may ways to do it, for example, if you are familiar with C, you might like to use printf() or sprintf().

    I will show you another way, simply using string formatting :

    my $number = 10.8766756; my $string = $number . ""; # force $number into a string $string =~ s/(\.\d)\d*/\1/; # if $string has one (or more) decimal # figures, replace them all by the first decimal figure

    Another one is the purely mathematical method, which might (or might not) work, depending on the size of your numbers (notice that arithmetic with floating point numbers is not always exact) :
    The int() function returns the integer part of a number, so we simply calculate

    $string = int( $number * 100 ) / 100;
    which will also give you 10.8, but note that the number is not rounded but truncated to 0, as the int documentation will tell you.

      that the number is not rounded but truncated to 0, as the int documentation will tell you.

      You can get around the truncation problem fairly easily with:

      $string = int( ($number * 100) + 5 ) / 100;
      Update: Well Alright then for the pedantic among us, see below ;-) (no offence meant, please don't take any)
      my $dec = 100; # Change as appropriate to give you the number # of decimal places you want int( ($number * $dec) + int($number * $dec) % 2 ? 0.5 * $dec : 0) / $dec
      That should sort out the rounding towards an even number.

      Nuance

        While this method is quite slick, it won't work as expected with numbers that are of the form 0.5, as it will always round up to the next number instead of rounding to the nearest even number (a rule which tries to make sure that most rounding errors cancel each other).

        Update: nuance has updated the above post, so this post has become meaningless ;).

        Here is an example for a case when always rounding up and using the results in further calculation. Adam calls this "premature rounding", but I think there are valid cases to do this, for example when doing accounting or printing invoices. In this example, using the rounded values in further calculations increases the error introduced by rounding :
        Consider (for simplicity) 2.5 * 3.5 :
        MethodResultError
        exact8.750
        round up12.003.25
        round to nearest even number8.000.75
        In this (extreme) example, the rounding errors don't cancel each other out when always rounding up. There are also cases where rounding up and rounding to the nearest even number give the same rounding error (consider 3.5 * 1.5).

        And by the way, I'm not pedantic, I'm just trying to be accurate :)

RE: Numbers....
by muppetBoy (Pilgrim) on Jun 02, 2000 at 18:26 UTC
    You could try something like:
    $a = 10.8766756; $b = sprintf("%.2f", $a);
    This will round the number up/down. The 2f means to two decimal places so in this case you will get 10.88
    Rounds up if next digit is >= 5.
Re: Numbers....
by ZZamboni (Curate) on Jun 02, 2000 at 19:47 UTC
Re: Numbers....
by jjhorner (Hermit) on Jun 02, 2000 at 18:31 UTC

    Try something like this:

    printf "%10.2f\n", $variable;

    This will print $variable in a 10 character field with 2 places after the decimal. Change the formatting numbers to suit your needs (%.1f would fit above).

    The 'f' tells you the number is a floating point.

    Chapter 6 of _Learning Perl_ gives a few more examples.

    J. J. Horner
    Linux, Perl, Apache, Stronghold, Unix
    jhorner@knoxlug.org http://www.knoxlug.org/
    
Re: Numbers....
by Apterigo (Scribe) on Jun 03, 2000 at 00:11 UTC
    If you wanted to cut a number like 10.8766756 down to 10.8 why not use the substr function? You obviously don't want to simply round it (because that would be 10.9), so you could simply say:
    $a = 10.8766756; $a = substr($a, 0, 4);
      What if your number is 191928219.19292?

      $a = 1034341.8766756; my ($l, $r) = split /\./, $a; $a = substr($a, 0, (length $l) + 2);

      There is probably a bunch of ways to do this. Benchmarking them would be good.

      Cheers,
      KM

        You could also be clever and do this with a regex. (I've actually done this!)

        ($trunc_num) = ($fullnum =~ /(.*\.\d\d)/);

        This will take all the values up to the decimal, the literal period, and the next two digits, and stick them in $trunc_num.

        You might want to be sure you have the right number of digits, so that something that's a buck fifty comes out as 1.50 rather than 1.5 - there are also lots of ways to do this. I tend to, being lazy, do something like:

        $fullnum .= "00" if $fullnum =~ /\./;

        $fullnum .= ".00" unless $fullnum =~ /\./;

        And then do the regex trick. The check for the literal period takes care of integer values, like '5', so they don't become 500.

        Okay, it's not pretty, but it is sort of a nifty way to do it.

        -- Kirby

        Tuxtops: Laptops with Linux!