Übersicht Letzte Änderungen Neuigkeiten Suchen ElispSektion KurzAnleitung Impressum

RoundFloatingPoints

Sometimes you’re drawing things in Inkscape and want to use the result in some SVG generating script of yours. Getting rid of all the floating point numbers might be a way to “clean it up.”

This is what you have:

M -10.022167,-21.971396 C 14.7336803,30.5846502 49.14571,63.48349 49.41039,69.9259 -2.16273,1.28938 -5.00496,2.57876 -7.1677,3.86813 -12.05602,-22.84412 -32.72831974,-43.9280398 -46.32897,-62.8068 -9.89946,8.0545502 -34.92668,48.69872 -44.342,58.93586 -2.89316,0.0433 -5.78272,-0.31286 -7.89219,-1.54601 5.51129,-13.18057 47.80993,-63.6927 56.32047,-68.37708 38.391843,-37.769646 C 16.87704,30.2274202 51.65071,57.31859 51.61874,63.81045 -2.16273,1.28937 -1.77736,3.08837 -3.9401,4.37775 -12.25494,-21.6505498 -36.20863,-41.22266 -49.55657,-61.61769 -9.89945,8.05455 -24.3945,23.8971602 -33.8098297,34.1342902 -2.89316004,0.0433 -2.04550004,0.0269 -4.15497,-1.20625 C 9.1566233,-10.942046 29.881293,-33.085266 38.391843,-37.769646 z

Use this code:

(defun round-floating-points (start end)
  (interactive "r")
  (save-restriction
    (narrow-to-region start end)
    (goto-char (point-min))
    (while (re-search-forward "-?[0-9]*\\.[0-9]+" nil t)
      (replace-match (number-to-string
		      (round
		       (string-to-number (match-string 0))))))))

Mark region, run ‘M-x round-floating-points’ and this is what you get:

M -10,-22 C 15,31 49,63 49,70 -2,1 -5,3 -7,4 -12,-23 -33,-44 -46,-63 -10,8 -35,49 -44,59 -3,0 -6,0 -8,-2 6,-13 48,-64 56,-68 38,-38 C 17,30 52,57 52,64 -2,1 -2,3 -4,4 -12,-22 -36,-41 -50,-62 -10,8 -24,24 -34,34 -3,0 -2,0 -4,-1 C 9,-11 30,-33 38,-38 z