Rounding numbers to N decimals in Emacs

Gergely Polonkai
Oct 7, 2014 :: 10:28

I have recently faced a problem, where I had a bunch of SVG files with a large amount of fraction numbers in the path definitions. These images were displayed in small size, so this amount of precision was irrelevant, and these numbers took almost half of my SVG images’ size. So I created an Elisp defun to round these numbers to 2 decimals:

This finds the first digit of the number under point (the cursor), and reduces its digits to the given amount (or the number given with C-u). It has some drawbacks, though, as it cannot handle exponential forms (e.g. 1e-1234), but these were rare in my case, and its hard to iterate through all numbers. I will come over this latter problem soon(ish).

comments powered by Disqus

Gergely Polonkai is a systems engineer of a telco company, and also a freelancer self- and software developer.

He is learning about different IT subjects since the late 1990s. These include web development, application building, systems engineering, IT security and many others. He also dug his nose deeply into free software, dealing with different types of Linux and its applications, while also writing and contributing to some open source projects.

On this site he is writing posts about different stuff he faces during work (oh my, yet another IT solutions blog), hoping they can help others with their job, or just to get along with their brand new netbook that shipped with Linux.

“I believe one can only achieve success if they follow their own instincts and listen to, but not bend under others’ opinions. If you change your course just because someone says so, you are following their instincts, not yours.”