AleGore
AleGore

Reputation: 303

How do I format a number as decimal to store it in mysql?

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?

Upvotes: 2

Views: 6193

Answers (3)

maček
maček

Reputation: 77826

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.

Upvotes: 2

Mark Byers
Mark Byers

Reputation: 839254

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.

Upvotes: 1

animuson
animuson

Reputation: 54797

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

Upvotes: 2

Related Questions