webdevRefinery Forum: Handling money/currency with MongoDB - webdevRefinery Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

User is online Mo3 

  • Brogrammer
  • Group: Moderators
  • Posts: 1950
  • Joined: 21-July 10
  • LocationStuttgart, Germany
  • Expertise:PHP,Java,Javascript,Python,Ruby on Rails,Node.js

Posted 24 March 2012 - 09:16 AM (#1)

Handling money/currency with MongoDB


I'm trying to finish one of my bigger projects, and this one happens to use Ruby on Rails, PGSQL (only for the authentication system, I don't trust Mongo with that) and MongoDB.

These are examples for (fictive) user input:

  • 12301,12€
  • $124.97
  • -340.12€
  • -$1213.12


I need to store that in my MongoDB database. That should be quite easy, just store the string, right?

Well, no. I need to take, for example, "-$1213.12" and figure out if the amount is negative or positive and in which currency the amount is, and store it accordingly. Furthermore, I need to be able to pull out these entries later and use them for calculations. Another problem I discovered is that some countries use a dot to seperate decimal places, and some use a comma. Ruby can only handle dots.

Any suggestions, possibly?

EDIT// I solved one problem by banning currency symbols in the user input. That leaves me with the negative/positive problem and the seperator problem.

User input now may look like this:

-11238,12 USD

Quote

A person who thinks all the time has nothing to think about except thoughts. So he loses touch with reality, and lives in a world of illusion called the past. Things are not explained by the past, they are explained by what happens right now. That creates the past, and it begins here. That's the birth of responsibility.
0


User is offline DarkCoder 

  • Group: Members
  • Posts: 1463
  • Joined: 08-March 10
  • LocationEngland, United Kingdom
  • Expertise:HTML,CSS,PHP,Javascript,SQL

Posted 01 April 2012 - 05:46 AM (#2)

Use a dropdown box for the currency?
0


User is offline TheEmpty 

  • I say words in sequences.
  • Group: Members
  • Posts: 5154
  • Joined: 02-October 10
  • Expertise:HTML,CSS,PHP,Java,Javascript,Python,Ruby on Rails,SQL

Posted 01 April 2012 - 09:22 AM (#3)

Use localization from i18n library. Should allow you to take in all forms. Worse case scenario use regexp.
Reserved.
0


User is offline ianonavy 

  • Group: Members
  • Posts: 685
  • Joined: 14-April 10
  • Expertise:HTML,CSS,Java,Javascript,Python

Posted 01 April 2012 - 10:45 AM (#4)

You could have the smaller denominations in a separate textbox. When the user presses "," or "." appropriately, have it focus on the next box.
reputation += 1 if post.is_helpful else 0
0


Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Enter your sign in name and password


Sign in options
  Or sign in with these services