Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: In-place regex substitution

by AnomalousMonk (Archbishop)
on Dec 10, 2016 at 15:49 UTC ( [id://1177599]=note: print w/replies, xml ) Need Help??


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:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1177599]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2024-04-16 07:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found