Tricking MariaDB into correctly converting float to decimal

I recently had the problem that I needed to convert a float column to decimal in a MariaDB. Naive as I was, I just executed a modify statement over the table (in a test instance, of course) and turned said column into a decimal(65,30) (which is the default type you get when doing code-first migrations in .net).

Checking back with the column values, it turned out that MariaDB had a slightly different idea of value conversion when it heard "change to decimal!". My previously perfect round numbers like 1.67 or 19.53 suddenly turned to 1.6699999570846558 and 19.530000686645508. Yikes. But before we solve it, let's check where this is coming from.

If I had wanted to convert from float to double, it would've been all fine. My 1.67 would've stayed 1.67 and 19.53 just the same. But decimal is an issue, and it's not an SQL exclusive problem. You'll (in theory) encounter the same behavior in C#, but C# is a lot smarter in converting the values and will give you - to an extent - the correct number.
The issue is that float values are stored to the base of 2, whereas decimal values are stored to the base of 10. This allows the latter you give you highly precise data, while the former still has a problem with calculating 0.1 + 0.2 correctly.

So, something - whatever it is - in the conversion algorithm from base-2 to base-10 goes wrong in MariaDB, shrugs and goes "I tried my best" whilst giving you garbage back. But there's a hack with which you can trick it into actually doing what you want: converting it first to varchar and then to decimal.

ALTER TABLE MyTable MODIFY MyColumn VARCHAR(255);
ALTER TABLE MyTable MODIFY MyColumn DECIMAL(65,30);

It's both incredible and ridiculous that this works, but it does. It will in the end correctly store 1.67 and 19.53 in the respective columns. Took me a while to figure it out, but where there's a will, there's a way.