Jump to content


Photo

number_format in custom function


  • Please log in to reply
2 replies to this topic

#1 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 24 August 2006 - 07:45 PM

Hello All,

I'm trying to create the function named fm [money formatter] to, err well, format my money!

Hers is what I have:

<?php

function mf($money)
{
$symb = '£';
number_format($money);
echo"$symb$money";
}

//TO TEST BELOW//
Print"Testing: ";
mf(1000);

?>

Seems very simple, it SHOULD have formatted the number (ie 10000 = 10,000) and added a currency symbol before it.

However, the number_format part doesnt seem to work as I get results such as £10000 when I test, and not the £10,000 I was hoping for.

Anyone know what I did wrong or another way to do this?

Thanks in advance!

#2 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 24 August 2006 - 08:03 PM

Nevermind - worked out my simple mistake. Never pjut the numver_format into a usable variable.

<?php

function mf($money)
{
$symb = '£';
$formatted = number_format($money);
echo"$symb$formatted";
}

//TO TEST BELOW//
Print"Testing: ";
mf(1000);

?>


#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 24 August 2006 - 08:34 PM

You probably want to have your function return the string instead of echoing it.
<?php
function mf($mny,$symb='£') {
    return($symb . number_format($mny,2));
}
echo 'Testing: ' . mf(10000);
?>

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users