kirk112 Posted August 9, 2015 Share Posted August 9, 2015 Hi, Just seeing if there is a function or similar that I can use to format currency for styling purposes. All our products are stored in the decimal format in the database and can format them to the correct local, but for the design of the site I need to modify the display price if there is a pence value, so that the .99p has its own class. For example £120 I can display that correct with no decimal points etc but if the price is £79.99 I would want to break after the decimal point and add in tags for presentation display £79.<small>99</small> I have a function that explodes at the decimal point etc but was wondering if there is a simpler way to achieve this, as when the currency is EURO they have the decimal point at the thousand separator... Thanks Quote Link to comment Share on other sites More sharing options...
scootstah Posted August 9, 2015 Share Posted August 9, 2015 (edited) money_format() or number_format(). EDIT: I should read better. Those might not do what you want. Probably easiest to just use regex or what you're already doing. Edited August 9, 2015 by scootstah Quote Link to comment Share on other sites More sharing options...
Barand Posted August 9, 2015 Share Posted August 9, 2015 Perhaps function priceFormat ($val, $currency) { $vi = intval($val); $dec = $currency=='€' ? ',' : '.'; if ($val == $vi) { return "$currency$vi"; } else { return sprintf('%s%d%s<small>%02.0f</small>', $currency,$vi,$dec,($val-$vi)*100); } } echo priceFormat(10.00, '£') . '<br>'; echo priceFormat(10.99, '£') . '<br>'; echo priceFormat(120.00, '€') . '<br>'; echo priceFormat(120.99, '€') . '<br>'; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.