I’ve been making “Product Object” for my app and I’ve been setting attributes, when suddenly I got to price attribute and stumbled upon it with question what type better to choose? I understand, that for PHP it doesn’t matter so much, but lets say for MySQL it does.
Any suggestions?
I’ve been making Product Object for my app and I’ve been setting attributes, when
Share
Real number types are better for numbers because you can do proper comparisons with them (e.g., price < 10.0). However, they do introduce problems with precision (things like 3.0 = 2.9999999999). It’s best to use a native Decimal type in this case, but lacking that, I would store the prices as an integer number of cents (e.g., 6.77 => 677), then divide by 100 before presenting the number to the user. That way you get the number operations in the database, and you get the precision (assuming that precision of 1 cent is enough).