开发者

php handling money and money values

开发者 https://www.devze.com 2022-12-12 11:11 出处:网络
Are there any standards or rules to follow when handling money and denominations in php like .45c, .35c or $4.00 as values?

Are there any standards or rules to follow when handling money and denominations in php like .45c, .35c or $4.00 as values?

I don't want to do this 400 cents as a value for $4.00, I just wanted to use $4.00 dollars as is in php without modifications, likewise 开发者_JAVA百科.50c as is.


Usually fractions are expressed as $0.45 or £0.12.

Using number_format() in PHP can help you format the values any way you like. There is also money_format() that takes into account the locale settings to try and figure out how to display currency.

If you were to use cents, most currencies have different names for these as well as different formatting. So the easiest and safest way to use currencies is to take the currency code/name and keep all of the values in the same form.

Also make no mistake here, formatting the currency symbol is also very different in different countries. The symbol can be placed to the left or right, with or without a space and it can be one or several letters or symbols. Therefore it makes sense to use functions like money_format() if possible to take away a lot of the logic involved.

If you want to know how others handle it, take a look at some of the e-commerce platforms, such as Magento, Prestashop, Zencart, osCommerce etc. These also have libraries and functions to deal with currency display, and sometimes currency conversion as well.

References
Formatting money / currency | number_format() | money_format()

0

精彩评论

暂无评论...
验证码 换一张
取 消