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

Hi.. I'm learning Perl on my own, so I kinda miss out all the available functions.
Is there a way to specify decimal precision in a number after a mathematical operation??

ex: I have 12.3456789 that I want to turn into 12.34
for now I used a RegEx to do this  s/(\d+)\.(\d\d)(\d+)/$1\.$2/; but I'm sure that there is a better way of doing this. a function maybe ( and I think that regex is bad too )
any thoughts?
Chady | http://chady.net/

Replies are listed 'Best First'.
Re: Where's that function
by Trimbach (Curate) on Feb 04, 2001 at 23:17 UTC
    sprintf is what you want. sprintf lets you do lots of fun things to the format of numbers or strings, including rounding fractions. The docs are a wee bit confusing if you've never used printf before, so here's a shortcut:
    $number = 12.3456789; $rounded_number = sprintf("%.2f", $number); print $rounded_number; # prints 12.35

    Gary Blackburn
    Trained Killer

Re: Where's that function
by OeufMayo (Curate) on Feb 04, 2001 at 23:21 UTC

    You should read the sprintf to get exactly what you need, but a rough solution might be:

    $x = 12.3456789; $x = $sprintf('%.2f', $x);
    <kbd>--
    PerlMonger::Paris(http => 'paris.pm.org');</kbd>
      I think you meant to say
      $x = 12.3456789; $x = sprintf('%.2f', $x);
      , right? sprintf dosen't start with a dollar sign...
Re: Where's that function
by terb (Acolyte) on Feb 04, 2001 at 23:18 UTC
    You could use sprintf("%.2f", $n) to return $n with two decimal places
Re: Where's that function
by damian1301 (Curate) on Feb 04, 2001 at 23:24 UTC
    Hi.. I'm learning Perl on my own, so I kinda miss out all the available functions.

    Liar! You can never learn Perl on your own when you have that wonderful documentation around :). Just check out perlfunc and it lists all the functions for you with examples and such. You *could* learn Perl from the documentation, so use it as much as you can :)

    Wanna be perl hacker.
    Dave AKA damian

    I encourage you to email me
      Oh well.. you cought me... :)
      what I meant was that I'm not following any specific courses.. just the bits I get my hands on.. :)
      Chady | http://chady.net/
Re: Where's that function
by I0 (Priest) on Feb 05, 2001 at 05:56 UTC
Re: Where's that function
by Maclir (Curate) on Feb 05, 2001 at 02:27 UTC
    Since you are teaching yourself Perl, you really should get yourself a copy of "The Perl Cookbook" - another of the brilliant O'Reilly books. Pages 46 to 47 have exactly what you want.

    As well as covering the sprintf / printf functions which others have mentioned, it also covers the int, floor and ceil functions.

Re: Where's that function
by jepri (Parson) on Feb 05, 2001 at 06:49 UTC
    Fore!

    my $x=12.3456789; my ($a,$b)=split (/\./,$x); $b=substr $b,0,2; $x=$a.'.'.$b;

    ____________________
    Jeremy
    I didn't believe in evil until I dated it.

      Here we go jepri, I love golf =).. fore!
      my $num = 12.3456789; $num = substr($num, 0, index($num, '.') + 3); print $num; #prints 12.34

      Also, here's another that is probably overkill:

      use Math::Currency; my $num = 12.3456789; $num = Math::Currency->new($num);

      This handles the rounding up/down of the number, where the preceding golf attempt does not. The original post did not ask for rounding, though.

      this method is a little faster and shorter, but it dosen't work right when $x is negative:
      my $x = 12.3456789; $x = int($x*100)/100;