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

hello. i take some data from an excel file.they are numeric but i take them as string.i want to round them to 3 digits after decimal point. i use:
my $rnd=Math::Round::Var->new (0.001); $frsmt=@mtbf_data[3]; $rounded_percent_frsmt=$rnd->round($percent_frsmt);
in my computer, it works;but another computer it doesn't. i take "Argument "0,015233304" isn't numeric in sprintf at C:/perl2/site/lib/Math/Round/" error. when I add 0 to theese strings. the error message is gone .but still it's rounding to 0.00000 or 1.0000 or sth like that. thanks for your help.

Replies are listed 'Best First'.
Re: argument isn't numeric in sprintf
by Corion (Patriarch) on Jul 19, 2007 at 12:27 UTC

    You are falling prey to locale settings. You seem to have a German (or maybe other) locale where the decimal separator is "," (comma) not "." (dot). You will have to convert the data after you pull it out of Excel before you can work with it further:

    use strict; use warnings; use Math::Round; my $rnd=Math::Round::Var->new (0.001); my $frsmt = $mtbf_data[3]; # use "$" instead of "@" if you only extrac +t one value of that row $frsmt =~ tr[.][]d; # eliminate all dots from the number $frsmt =~ tr[,][.]d; # turn all commas into dots $rounded_percent_frsmt=$rnd->round($frsmt); print "$rounded_precent_frsmt\n";

    You should really, really get into the habit of using strict, because the code you posted didn't make much sense with the differing variable names ($frsmt vs. $precent_frsmt).

      $frsmt =~ tr[.][]d; # eliminate all dots from the number $frsmt =~ tr[,][.]d; # turn all commas into dots

      You have specified the /d option on the second tr/// but you aren't deleting anything.

      You can combine both statements with a single tr///:

      $frsmt =~ tr[,.][.]d; # eliminate all dots from the number and turn al +l commas into dots
      it works!! thanks a lot for your help