Jump to content

Archived

This topic is now archived and is closed to further replies.

master82

number_format in custom function

Recommended Posts

Hello All,

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

Hers is what I have:

[code]
<?php

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

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

?>
[/code]

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!

Share this post


Link to post
Share on other sites
Nevermind - worked out my simple mistake. Never pjut the numver_format into a usable variable.

[code]
<?php

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

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

?>
[/code]

Share this post


Link to post
Share on other sites
You probably want to have your function return the string instead of echoing it.
[code]<?php
function mf($mny,$symb='£') {
    return($symb . number_format($mny,2));
}
echo 'Testing: ' . mf(10000);
?>[/code]

Ken

Share this post


Link to post
Share on other sites

×

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.