如何将数字格式化为十进制以将其存储在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?

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.

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

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.