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

Hi,

Basically, I've got a number such as 141.32124355465 and would need to remove the decimals (everything after the dot, including the dot itself). I guess there's a way to do this with s///, although don't know how.

Thanks,
Ralph

Replies are listed 'Best First'.
Re: Removing decimals in number
by Limbic~Region (Chancellor) on May 14, 2004 at 18:46 UTC
    Anonymous Monk,
    Ok, for numbers it's simple and doesn't require regular expressions. You could just do:
    $number = int $number;
    Now if your question is how do I remove all the periods from a string, I would suggest:
    $string =~ tr/.//d; # or $string =~ s/\.//g;
    And finally, if you want to remove everything from the first ocurrence of something to the end of the string:
    $string = substr( $string, 0, index($string, '.') ); # or ($string) = $string =~ /^([^.]*)/;
    Cheers - L~R
Re: Removing decimals in number
by Belgarion (Chaplain) on May 14, 2004 at 18:48 UTC

    You have a couple of options. The one I would go with is the int function. Like so:

    print int('141.32124355465'); __OUTPUT__ 141

    You could use a regular expression like:

    my $num = 141.32124355465; $num =~ s/\.\d+$//; print $num; __OUTPUT__ 141
Re: Removing decimals in number
by sacked (Hermit) on May 14, 2004 at 19:08 UTC
    You can also use printf:
    printf "%d" => 141.32124355465 # prints 141

    --sacked
      Except that doesn't always work:
      $ perl -e 'printf "%d\n", 141.999999999999999' 142
      int is the right way to go. Or even POSIX::floor
Re: Removing decimals in number
by Anonymous Monk on May 14, 2004 at 19:08 UTC
    Thanks for your replies :-) I used the int() function. However, is there any resource where I can learn more about writing my own regular expressions using s///?

    Thanks,
    Ralph