Jump to content

Formatting Currency


kirk112

Recommended Posts

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 

 

 

Link to comment
Share on other sites

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>';

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.