Monday, January 09, 2012

How to multiply by 100

The following horror story is based entirely on true events. Programmer discretion is advised.

Imagine a scenario where you have a number with 2 decimal places. Say, 799.00. But right at this moment, you don’t want that decimal point to be there.

In an ideal world, you would just multiply by 100 or something. The rest of humanity has done this for centuries without incident.

But this is not an ideal world. Far from it. This is a terrifying, cruel world with the madness pouring out of it like a burning clown juggling flamethrowers inside a cuckoo clock.

So, in this terrifying world, here’s how you do it:

First, you need to ensure that there’s a decimal point. (But of course!)

This is best accomplished by the brilliant method of first converting a calculated sum to an integer (no don’t leave!), then converting it into text and then slapping a “.00” to the end. Then, finally, reconverting the whole thingama back to a float.

There you go. There’s your decimal point. Stay with me here.

Now that all traces of logic and reason have been completely vaporized and teleported to another dimension, we may proceed.

The next step is to search and verify if there does indeed exist a decimal point - disregarding the 5 lines of code you just wrote where you manually formatted that crap to include a decimal and two zeros at the end.

(Hey, jinnis exist, and they can mess with your variables and shit)

Once you have confirmed that a decimal place does indeed exist (what are the chances?!)  the next thing to do is to figure out the portion that precedes it. In a sane, logical world, you might accomplish this by using something like a simple truncate function. But hey, where’s the fun in that?

Instead, you take that float, and convert that shit right back to text again.

Now, use a text function to return everything except the last three places. (ha! c’mon. it’s not like we don’t already know that they’re just the decimal point followed by the 2 zeros... bwahaa! LOL! shh! ahem *serious face*)

Now, store this returned value into a carefully hidden, completely anonymous textbox that will, at various points, store a variety of such temporary marvels.

Why? Because, fuck variables. That’s why.

But coming back to the text.. Come to think of it. What if the two digits after the decimal point weren’t zeros? I mean, we did manually stick that “.00” to the end. But who knows?! (See: The aforementioned Jinnis)

Indeed. So with just that scenario in mind, we use another text function to retrieve the last two digits. And..wait for it... append it to whatever was in the textbox.

The decimal point is now history.


“But”, you might wonder, “couldn’t you just have removed the decimal place with a simple replace method - or even better, never have put in the darn thing in the first place?”

In fact, why are we doing any of this?

Because. The world has gone utterly, irrevocably mad. That’s why.

If, at this point, you haven’t 
already pulled a gun on yourself, congratulations. You could be the next Dalai Lama.

_

*sigh* Fun times maintaining old code.

Why won’t anyone kill me already?



~Peace!
Previous Post Next Post Home