0

I have a game I am working on and am storing the amount of money as a single number instead of pieces of gold, silver and copper.

E.g PlayerX has 123 gold, 45 silver and 67 copper In this instance the database entry would store 1234567 as the money value. 100 copper = 1 silver and 100 silver = 1 gold

Is there an easy way to convert the long number into gold, silver and copper values easily?

1
  • 5
    Will you never want to know how much silver or copper a player has? Because if you ever do, you should simply store these separately. You should really ask yourself, "what advantage do I gain by combining them to justify the increased complexity that arises from doing so?" Commented Jan 28, 2012 at 19:20

1 Answer 1

3

Use the modulus operator %:

$copper = $money % 100;
$silver = floor( ( $money % 10000 ) / 100 );
$gold = floor( $money / 10000 ); 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.