Reputation: 5588
I have this piece of text:
(-1.000mm 1.000mm)
(-0.500mm 0.800mm)
(0.500mm 1.200mm)
(1.000mm 1.000mm)
(0.800mm 0.500mm)
(1.200mm -0.500mm)
(1.000mm -1.000mm)
(0.500mm -0.800mm)
(-0.500mm -1.200mm)
(-1.000mm -1.000mm)
(-0.800mm -0.500mm)
(-1.200mm 0.500mm)
(-1.000mm 1.000mm)
I would like to multiply all of these numbers by 0.95 (and other numbers). What I tried is:
:%s/[0-9.]*mm/\=submatch(0)*0.95/g
but the result is:
(-0.95 0.95)
(-0.0 0.0)
(0.0 0.95)
(0.95 0.95)
(0.0 0.0)
(0.95 -0.0)
(0.95 -0.95)
(0.0 -0.0)
(-0.0 -0.95)
(-0.95 -0.95)
(-0.0 -0.0)
(-0.95 0.0)
(-0.95 0.95)
I must be doing something wrong with the grouping or something because besides most numbers turning to 0.0, the mm
falls away.
Upvotes: 2
Views: 244
Reputation: 27283
The problem is that your floaty strings are coerced to integers during multiplication, not floats. The second issue is that you replace the mm:
:%s/[0-9.]*\zemm/\=str2float(submatch(0))*0.95/g
The str2float
function turns a string into a float. The \ze
regex special sequence "matches at any position, and sets the end of the match there."
Result:
(-0.95mm 0.95mm)
(-0.475mm 0.76mm)
(0.475mm 1.14mm)
(0.95mm 0.95mm)
(0.76mm 0.475mm)
(1.14mm -0.475mm)
(0.95mm -0.95mm)
(0.475mm -0.76mm)
(-0.475mm -1.14mm)
(-0.95mm -0.95mm)
(-0.76mm -0.475mm)
(-1.14mm 0.475mm)
(-0.95mm 0.95mm)
Upvotes: 5