Author Topic: Currency (variable)  (Read 671 times)

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Currency (variable)
« on: September 29, 2011, 06:52:45 AM »
The price is $4,99, how to store it?

1) Make 2 separate variables dollars and cents $4,99 = 4 and 99.
2) Make one variable and store everything as cents $4,99=499.
3) Use float (but IIRC you can't make a perfect representation of 0.01 in float so it can't be used?)

I'm ignoring all heavy blown solutions like math extension. It has to be simple, small, easy.

Offline Harkins

  • Level 28
  • **
  • Posts: 424
  • Reputation: +11/-2
  • Coder, blogger, entrepreneur.
    • View Profile
    • Push CX - Blog
Re: Currency (variable)
« Reply #1 on: September 29, 2011, 09:09:22 AM »
Store everything as cents, show dollars and cents to users. Floats will have rounding errors, yes.

Visit #bbg on irc.freenode.net to talk browser games anytime.

Offline Nox

  • Level 35
  • **
  • Posts: 767
  • Reputation: +12/-2
    • View Profile
Re: Currency (variable)
« Reply #2 on: September 29, 2011, 09:15:36 AM »
Agree
just note: as far as database goes, MySQL's DECIMAL should handle it precisely

btw. if you don't want a heavy solution you probably don't care about 0.0000000000013 difference anyway :)
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline BlackScorp

  • Level 15
  • *
  • Posts: 123
  • Reputation: +6/-0
    • View Profile
    • Cruel Online
Re: Currency (variable)
« Reply #3 on: September 29, 2011, 10:51:51 AM »
Story everythink as Strings and you wont have any problems:D

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: Currency (variable)
« Reply #4 on: September 29, 2011, 12:35:01 PM »
Story everythink as Strings and you wont have any problems:D
People, toss me some wood, we have a performance heretic to burn here :D

I did some test and float lose precision of 1 cent after 15000 transactions (+1 cent)... I guess it's acceptable for a game :)

I also googled that there is some "money" data format in SQL (two versions, 8 or 4 byte). Anyone used it?

Offline pirategaspard

  • Level 9
  • *
  • Posts: 46
  • Reputation: +1/-0
    • View Profile
    • PointClickPress
Re: Currency (variable)
« Reply #5 on: September 29, 2011, 12:46:52 PM »
In college we were taught that when it comes to money, never store it using any kind of float. users won't appreciate the inaccuracy. Store in cents using an int field and then format to display.  In certain circumstances I've also used strings to store money with the decimal as a string field will also be accurate.  I don't have any experience with the money field type though.

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: Currency (variable)
« Reply #6 on: September 29, 2011, 02:38:31 PM »
I've also used strings to store money 
More wood, bring more wood. That's the problem with heresy, unless you root it out early it starts spreading :)

Offline pirategaspard

  • Level 9
  • *
  • Posts: 46
  • Reputation: +1/-0
    • View Profile
    • PointClickPress
Re: Currency (variable)
« Reply #7 on: September 29, 2011, 02:50:54 PM »
On a large international CMS application for marketing products....
Parts of the world use:  1,000,000.00
Other parts of the world use: 1.000.000,00

The problem was described as the proverbial Russian pencil in space vs. the USA million dollar space pen: yeah we could've built out a system to accept the different formats from each locale then convert to int and then format back to display, but wasn't really useful since it was display-only data. So we stored prices as string.

:P Projects gotta meet deadlines (occasionally)

Offline Kyle

  • Level 10
  • *
  • Posts: 56
  • Reputation: +0/-0
    • View Profile
    • The Best Text Games
Re: Currency (variable)
« Reply #8 on: October 24, 2011, 04:27:30 AM »
Use DECIMAL(11,2). Problem solved.
Do you own a text based game?
Add it to my list of text based games for free promotion! BestTextGames.com

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal