Mini-Tutorial: clipping / rounding decimals (1 Viewer)

Rudgar

Content Creator
Joined
Nov 18, 2016
Ahoy,
because I had to deal with it in my own project plus I discovered that @WeeWillie has similar "problems" in his SlaveBazaar, I'd like to share a little "trick" (mathematical knowledge) to avoid an endless number of decimals in dialogues like this:
screenshot0002.png
Of course one could think about using the DA-Operators "%" (modulo = remainder of a division) and "\" ("floored value" or "integer division", more or less the opposite of modulo) for this which is only one half the truth.
Using modulo and integer division doesn't help that much at first:

*37 \ 13* results in 2
*37 % 13* results in 11
...
nah, not helpful. We wanted some decimals - not "all or nothing".

But now comes the little trick: multiplying the result of the division by a multiple of ten before clipping all decimals ("\ 1" = "integer clipping") saves the wished number of decimals:

*37 / 13 x 100 \ 1* results in 284 - all we have to do is ... getting our decimal point back.

all to easy: just divide by the same multiply of ten:

*37 / 13 x 100 \ 1 / 100* results in 2.84 or
*37 / 13 x 100000 \ 1 / 100000* results in 2.84615 in case you want five decimals instead of just two.
Great!
Done?
Nope.
Our result is just clipped, not rounded.
When shortening the result of our division to two decimals, it should result in ... 2.85 (instead of our clipped 2.84).
For those who don't know / rememeber:
we round up if the next digit is greater than 4
we round down if the next digit is less than 5

Now how can we do that?
Still easy: we add 0.5 before "integer clipping"

*37 / 13 x 100 + 0.5 \ 1 / 100* results in 2.85

Et voilà: the trick is done, the magic happened, the miracle appeared. :cool::wink:

If you still have questions, don't hesitate to ask. It's just a little something that crossed my mind and of which I thought it could be helpful for others sometimes.

Respectfully,
Rudgar
 

Attachments

decimals.txt
770 bytes · Views: 130

Last edited:

Users who are viewing this thread

Top


Are you 18 or older?

This website requires you to be 18 years of age or older. Please verify your age to view the content, or click Exit to leave.