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

Newbie question: I have various numbers that need to be put in xxx.xx form.

13.2 needs to be 13.20
13.235 needs to be 13.24
13 needs to be 13.00
13.63 remains 13.63

I know how to round the longer decimal numbers, but don't know how to get the zero value decimal places to display. (Is there a cast operator I can use, as in C?) Thanks

Replies are listed 'Best First'.
Re: decimal placement
by Chady (Priest) on Mar 26, 2001 at 01:35 UTC

    use sprintf like this :

    $n = 13.2; $n2 = sprintf("%.2f", $n); print $n2; # should print 13.20

    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.
Re: decimal placement
by arturo (Vicar) on Mar 26, 2001 at 01:39 UTC

    There's a printf function, as in C, and a sprintf function too.

    So

    printf ("%3.2f", $num);

    Should get you what you're after.

    HTH Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: decimal placement
by knight (Friar) on Mar 26, 2001 at 01:40 UTC
    The usual way to print floating-point values rounded and padded to two decimal places:
    printf "%.02f\n", $float
    Or, if you need to use a so-formatted string representation of your floating point number for something else:
    $string = sprintf "%.02f", $float
    Read up on s?printf in the doc for a full description of what you can do with the formatting string (the %.02f part of the above examples).
Re: decimal placement
by lachoy (Parson) on Mar 26, 2001 at 03:59 UTC

    In addition to s/printf, you might also want to check out Number::Format, which deals with localization issues as well as other formats like bytes/kilobytes/megabytes, money, etc.

    Chris
    M-x auto-bs-mode

      This may be seriously unfair, but the two aspects of Perl's behaviour (to my eyes bugs) that I dislike most I learned tracking down bugs in Number::Format.

      The first was the infamous "my $foo if 0;", the other is the fact that $_ is the same in all namespaces, so call a function in a map and it can wipe out your input array - even if the module is in another namespace.

      Not bad for a module that I don't even use! (Co-workers discovered it and used it. Personally I would have been perfectly happy with what sprintf offers...)

Re: decimal placement
by Anonymous Monk on Mar 26, 2001 at 01:42 UTC
    Why are you all using double quotes in your printf formats? You aren't using any interpolation, so you should use single quotes instead (it's faster because it dosen't have to check for interpolations)
      My recollection - which may be wrong - is that in Perl 5 there is no real difference between single and double quotes. At compilation it figures out whether there is any interpolation.

      In which case the difference between using double and single quotes is basically irrelevant. Use whichever one you find clearer.

        See this for benchmarks on exactly how little difference this type of change makes.

                - tye (but my friends call me "Tye")
        this is false. single quotes are literal quotes meaning do not interpolate ever. double quotes allow interpolation, though in programming perl they are also listed as "literal" they are literal w/ interpolation (pg. 41, Programming Perl 2nd ed.).

        --caesium

        capitalization takes away valuable keystrokes... my doctor recommends no more than 5x10^50th keystrokes per day

Re: decimal placement
by Fingo (Monk) on Mar 26, 2001 at 02:52 UTC
    If you have many numbers, format may be a good option.