You could even reduce that to an in-place substition that would leave non-valid "fraction" strings unchanged:my $fractionString = "1/2"; my $fractionValue = ( $fractionString =~ m{^(\d+)/([1-9]\d*)} ) ? $1/$ +2 : "undefined"; print "$fractionString is actually $fractionValue <br>";
$fractionString =~ s{(\d+)/([1-9]\d*)}{$1/$2}e;
(update: In view of the (seemingly picky but admittedly valid) concerns expressed below by tilly, I should point out up front that my suggestions here are extrapolating from the particular case given in the OP, to extend to all and only positive fraction strings. If you want to handle negatives or other embellishments, further work is needed. Other solutions are possible that do not extrapolate, or that make different extrapolations, YMMV, etc, and good luck with all that. ;-)
In reply to Re: how to convert fractional string to decimal numbers ?
by graff
in thread how to convert fractional string to decimal numbers ?
by adrive
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |