0

How to store currency values with commas in them? I'm working with phpMyAdmin with mySQL and Apache to make a database and I need to enter currency values with commas in them and also % values. Is there a datatype other than Varchar that I could use? BTW I'm very new to Php and SQL so don't yell at me. Thanks for your help!

NeonFlames
  • 3
  • 1
  • 4
  • http://dev.mysql.com/doc/refman/5.0/en/data-types.html – naththedeveloper Jul 16 '14 at 15:43
  • 1
    possible duplicate of [Best data type to store money values in MySQL](http://stackoverflow.com/questions/13030368/best-data-type-to-store-money-values-in-mysql) - short answer is `DECIMAL(10,2)` – naththedeveloper Jul 16 '14 at 15:47
  • 3
    Don't do it: store numbers properly with a decimal point; and only add % or change decimal point to decimal comma for display purposes: note that monetary values are best stored as integer rather than float – Mark Baker Jul 16 '14 at 15:47
  • @FDL thanks I didn't realize there was another form probably because I didn't specify that I wanted to store money values in my question – NeonFlames Jul 16 '14 at 16:00
  • `How to store currency values with commas in them?` sounds like you're specifying money values to me – Mark Baker Jul 16 '14 at 17:34

1 Answers1

1

$You could replace commas (,) with points (.) and use the data type float/double. This only works ofcourse if you do not use commas as 1k seperators.

http://php.net/manual/en/function.str-replace.php

$numP = str_replace(",", ".", $numC);

You can explicitly parse the number afterwards.

$numF = floatval($numP);

doubleval() is an alias. I do not know if this is best practice, but it works. however, you should not use this for financial applications.

oshell
  • 8,923
  • 1
  • 29
  • 47
  • How would I store the value if I am using commas as 1k separators should I just delete them completely? Sorry for the dumb question – NeonFlames Jul 16 '14 at 16:08
  • 1
    separators for thousands are not part of the number, they're formatting for display purposes.... remove them.... if you have commas as both thousands separators and as a decimal separator, then you have much more of a problem – Mark Baker Jul 17 '14 at 10:27