Jump to content
Larry Ullman's Book Forums

Database Values With Cents (Doing All This In Yii Of Course)


Recommended Posts

If you are using cents to work out the monetary terms should we save the database values like shipping costs, product prices and charges as decimals. Because if i am just pulling and displaying data from the database it would be easier rather than saving everything in cents. And if i need to use cents just change the decimal values into cents while running the php scripts for calculations?


Actually do Stripe save their database values in decimals or cents that may help to answer the question?

Link to comment
Share on other sites

Save everthing as integers in the database, can you confirm that? I have more than 100 php script files to change now for this affect to take place. Is this what Stripe are doing? I wonder if ebay do that?


Antonio i will write a lovely cents formatting helper class with some public static methods. May be i will name the class Cents. :)

Link to comment
Share on other sites

I do that myself. You could possible use Strings too, but I wouldn't like the need for conversions. As stated, when it comes to money, you'd want as few possibilities for errors as possible.


I'm also currently using an unimmutable Money class that I trust for interactions with Stripe. That way, I can perform addition and subtraction (Vouchers, shipping, summing totals for products, etc) in a Checkout object instead of using changable primitive types. The only place I use the amount as an integer is in a Stripe class that handles the actual payments. That way, what I put in stays in.


You you read about Fowler's Money Pattern before you do anything else. You'd want to encapsulate both the amount and a Currency instead of using primitive integers in PHP.

  • Upvote 1
Link to comment
Share on other sites

Thanks for your comments ill take a look into that. Larry's new book contains cents now, it seems we are all learning new things every day. Just a shame i didn't know that a year ago, well this the fun of sharing on this forum. This place can really get me confused sometimes, just like today.


Just had a read of Effortless E-Commerce V2, Larry done the formatting in SQL if the price needed to be directly brought the page. He didn't have any classes but used the php format function to convert the value if it was price if it was brought in as an integer to PHP then needed formatting. Whichever way you do it the job is done.


I think im going to build my own little Edward's Cents formatting class using some of the idea's from Fowler's class, i want to keep this as simple as possible. I know i will recode the whole site later anyway. Just waiting for Yii 3 to come, hahaha


Thanks Larry and Antonio... :wub:

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...