Friday, June 14, 2013

To format numerical value as swedish currency using PHP

You can use php function money_format() that returns a formatted version of number.
Note: The function money_format() is only defined if the system has strfmon capabilities. For example, Windows does not, so money_format() is undefined in Windows.
I needed a price to be formatted as Swedish currency. So I need to make my own function which should work. So I came up with this piece of code which converts a numerical string to swedish currency format.

$price = 10000;
echo changeCurrencySwedish($price); //outputs 100,000

function changeCurrencySwedish($price)
{
 $price_array = str_split(strrev($price), 3);
 return strrev(implode(",", $price_array));
}

No comments:

Post a Comment