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

Hello everybody, I need to strip "90.00" back to "90". I wrote the following program #!/perl $item = "90.0"; print &strip_dot($item); sub strip_dot { print "1 - $_[0]\n"; if ($_[0] =~ m/./) { print "2 - $_[0]\n"; return substr ($_[0], 0, index($_[0],".")); } else { print "3 - $_[0]\n"; return "$_[0]"; } } its output is: 1 - 90.0 2 - 90.0 90 Great that works. but if I make $item = "90"; the output is: 1 - 90 2 - 90 9 Hmm, that is not what I want. The if statement should generate FALSE and it shouldn't be stripped to + 9. Any help is appreaciated.

Replies are listed 'Best First'.
Re: stripping "." from amounts
by FunkyMonk (Bishop) on Jul 20, 2007 at 21:12 UTC
    Checkout printf and sprintf:
    my @data = qw/ 1 1.01 2.49 3.48999 3.5001 /; printf "%s => %0.2f\n", $_, $_ for @data;

    my @data = qw/ 1 1.01 2.49 3.48999 3.5001 90 90.0 90.00 90.01 /; #use %d instead of %0.0f if you don't want rounding printf "%s => %0.0f\n", $_, $_ for @data;
    As to your code, . in a regex matches any one character, see perlre and perlretut. You need to escape it: /\./

    update: misread the question. Code updated

Re: stripping "." from amounts
by CountZero (Bishop) on Jul 20, 2007 at 22:15 UTC
    What is wrong with using the int function to strip the decimal part of a floating point number?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Be careful when doing that; using int to round reduces portability:

      int EXPR

      int

      Returns the integer portion of EXPR. If EXPR is omitted, uses $_. You should not use this function for rounding: one because it truncates towards 0, and two because machine representations of floating point numbers can sometimes produce counterintuitive results. For example, "int(-6.725/0.025)" produces -268 rather than the correct -269; that's because it's really more like -268.99999999999994315658 instead. Usually, the "sprintf", "printf", or the "POSIX::floor" and "POSIX::ceil" functions will serve you better than will int().

        True, but one should never use int to round a value, just to get the integer portion. The manual is very clear on that and the OP did not ask for rounding.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: stripping "." from amounts
by graff (Chancellor) on Jul 21, 2007 at 04:58 UTC
    This line of your code does not do what you think it does:
    if ($_[0] =~ m/./)
    The unadorned period character in a regex is a "wildcard": it will match any single character (including a literal period, of course, but also a space, any digit, letter, punctuation mark, control code or any other symbol). If you put a backslash in front of the period in the regular expression, it will match only the literal period.

    The topic of special characters (and how to "escape" them so that they are not treated as "special") is pretty basic stuff you have to learn about regular expressions. Have you looked at perlretut yet?

    (But apart from that, I agree with others that there are better ways to remove the fractional part of a numeric string.)

Re: stripping "." from amounts
by swampyankee (Parson) on Jul 21, 2007 at 14:15 UTC

    If your values are always going to have ".00" to the right of the decimal point, you could use a regex on the stringified version of $number:

    $number =~ s/\.00$//;
    optionally replacing the second 0 with a plus sign or even an asterisk, depending on how many zeros you expect.

    Of course, I wouldn't do it that way; I'd use sprintf to round the value to an integer, because I'd want to avoid having 89.99999 slip through.


    emc

    Information about American English usage here and here.

    Any New York City or Connecticut area jobs? I'm currently unemployed.

Re: stripping "." from amounts
by oxone (Friar) on Jul 21, 2007 at 14:10 UTC
    You could also throw away a dot and anything following it with a regex like so:
    my $item = "90.00"; (my $stripped_item = $item) =~ s/\..*//;