in reply to Re: In-place regex substitution
in thread In-place regex substitution
$content =~ s/(\d\d\.\d*)/sprintf("%.6f",$1);/ge; #round to 6 decimal +digits
One can avoid hard-coding the rounding precision by using either
my $n = $some_integer;
... sprintf("%.${n}f", $1) ...
or the "wildcard" (if that's the right term (update: maybe "placeholder"?)) format specifier
... sprintf('%.*f', $n, $1) ...
Update: In the code above, I've implied that $n must be an integer. Interestingly (for some definition of "interesting"), if it is not, then the first code example ($n interpolated) screws up, but the second (* wildcard specifier) behaves "properly", DWIMishly ignoring the fractional part of the numeric value. (I haven't checked yet to see if this behavior is the same as in C/C++/D/etc.)
Give a man a fish: <%-{-{-{-<
|
|---|