MMO Currency - Making 493205 be 49g 32s 5c

48 Views Asked by At

Stackoverflow

This is most likely a very, very simple solution but my tired brain simply can't come up with it.

As the title suggests, I'd like to write a function that's able to convert a number like:

493205

Into a string of:

"49g 32s 5c"

What would be the most logical way of doing this?

1

There are 1 best solutions below

0
04FS On

Quick one-liner, assuming $x holds the integer value:

printf('%dg %ds %dc', $x / 100 / 100, $x / 100 % 100, $x % 100);

modulo 100 gives us the last two digits, and dividing by 100 “removes” the last two digits from the number. (Technically, it gives a float, but using modulo on that again forces integer conversion first.)