I am working on a PHP
script that will be used as part of an RPG. The value of money is stored as an integer but needs to be presented slightly differently.
For example if the value is 125050
then it should be shown as 12 gold, 50 silver and 50 copper
. However to complicate matters further sometimes the code will be used for a different system where 10 of each is equal to the next so 125050
would be 1250 gold, 5 silver (and no copper)
. Also some sessions will call for the use of a fourth currency unit (platinum, probably) which will follow the same pattern.
Given that I know that unit divisor is going to be 10 or 100 I need a method of presenting the integer value in the correct format. How can do this?
I did toy with the idea of casting the int to string and picking of the characters but I would rather use a purely mathematical approach so that I can also separate the int into an array of int like this:
// 125050
// =
$money(
'copper'=>50,
'silver'=>50,
'gold'=>12,
'platinum'=>0
);
Aucun commentaire:
Enregistrer un commentaire