2

I want to store a price for an item in decimal in MySQL.

How would I have to format the price in order to store in in the db?

1
  • What client language are you using? Commented Mar 3, 2010 at 20:17

3 Answers 3

2
number_format($number, 2, ".", "");

Or if you want the commas in it, you can use this one:

number_format($number, 2, ".", ",");

Check out number_format() at PHP.net

Sign up to request clarification or add additional context in comments.

Comments

2

Mark Byers is correct; you shouldn't need to modify the data before sending it to mysql.

to update your mysql column:

alter table foo change price price decimal(10,2) not null default 0

If your price column is currently an int, you will not lose data with this query.

For example, a value of 10 will be updated to 10.00, or a value of 5125 will be updated to 5125.00, etc.

Comments

1

You shouldn't need to do any formatting to store the data.

You can set the column type to decimal and do the formatting only when you display the value in the client, not when you store it.

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.