0

i've got this function where i want to format a number, for example, if it's integer like 79, the function returns it as $79,000,000.00. Otherwise, if it's 79.5, this function returns it as $79,500,000.00.

The function is the following and it's working fine, you can test it on http://www.compileonline.com/execute_php_online.php:

function format_bolsa($number)
{
    if(is_int($number))
    {
        echo '<p style="color:#0B3B0B">I am an INT and the number/value before processing is: ',$number,'</p>';
        $number='$'.$number.',000,000.00';
        echo '<p style="color:#0B3B0B">I am an INT and now the formatted number is: ',$number,'</p>';
        return $number;
    }
    else if(is_float($number))
    {
        echo '<p style="color:#0B3B0B">I am a FLOAT and the number/value before processing is: ',$number,'</p>';
        $number_tmp=explode('.',$number);
        $number='$'.$number_tmp[0].','.$number_tmp[1].'00,000.00';
        echo '<p style="color:#0B3B0B">I am a FLOAT and now the formatted number is: ',$number,'</p>';
        return $number;
    }
    echo '<p style="color:#610B0B">Nothing has been done. The number is: ',$bolsa,' but such a value will not be returned!</p>';
}

$number=79;

$number=format_bolsa($number);

echo $number;

Nevertheless, when i insert it inside a class like this (becoming a method):

public function format_bolsa($number)
{
    if(is_int($number))
    {
        echo '<p style="color:#0B3B0B">I am an INT and the number/value before processing is: ',$number,'</p>';
        $number='$'.$number.',000,000.00';
        echo '<p style="color:#0B3B0B">I am an INT and now the formatted number is: ',$number,'</p>';
        return $number;
    }
    else if(is_float($number))
    {
        echo '<p style="color:#0B3B0B">I am a FLOAT and the number/value before processing is: ',$number,'</p>';
        $number_tmp=explode('.',$number);
        $number='$'.$number_tmp[0].','.$number_tmp[1].'00,000.00';
        echo '<p style="color:#0B3B0B">I am a FLOAT and now the formatted number is: ',$number,'</p>';
        return $number;
    }
    echo '<p style="color:#610B0B">Nothing has been done. The number is: ',$number,' but such a value will not be returned!</p>';
}

the now method is not working properly and i get my custom message

Nothing has been done. The number is:',$number,' but such a value will not be returned!

I call the method like this:

$number=self::format_bolsa($number);

I don't know why it isn't working. I have inserted other functions like this and they're working fine now as methods.

Anyone can shed some light?

tereško
  • 58,060
  • 25
  • 98
  • 150
Pathros
  • 10,042
  • 20
  • 90
  • 156

2 Answers2

1

Why not simply use number_format?

echo number_format(79 * 1000000, 2);
echo number_format(79.5 * 1000000, 2);

Else..here is a working example to help you with your static method:

class Test
{
    public static function format_bolsa($number)
    {
        if (is_int($number)) {
            echo 'I\'m an int';
        }
        else if (is_float($number)) {
            echo 'I\'m a float';
        }
    }
}

Test::format_bolsa(79);
Test::format_bolsa(79.5);

Now with number_format:

class Test
{
    public static function format_bolsa($number)
    {
        return number_format($number * 1000000, 2)
    }
}

Also read why static methods are not good: static considered harmful

bitWorking
  • 12,485
  • 1
  • 32
  • 38
1

You should be calling:

$number=$this->format_bolsa($number);
Oli Folkerd
  • 7,510
  • 1
  • 22
  • 46