0

I am passing some variables to a function to round and add a dollars sign but when the values get into the function they have lost their values.

formatDollars($cost, $taxedCost, $shipping, $total)

function formatDollars($cost, $taxedCost, $shipping, $total) {
    $taxedCost = '$'.round($taxedCost, 2);
    $shipping = '$'.round($shipping, 2);
    $total = '$'.round($total, 2);
    $cost = '$'.round($cost, 2);
    return array($cost, $taxedCost, $shipping, $total);
}

list($cost, $taxedCost, $shipping, $total) = formatDollars();

When I output I get the dollar signs but all my numbers have become zero.

2
  • Please make an example how you use it, what output you get and what you would expect! formatDollars(); ?! Where are the arguments ? Commented Mar 26, 2015 at 13:57
  • 1
    You aren't passing the arguments in. Commented Mar 26, 2015 at 13:58

3 Answers 3

1

What you are doing is a very round-about way of handling this. You want to change the values of the parameters, so make them pass-by-reference.

formatDollars($cost, $taxedCost, $shipping, $total);
function formatDollars(&$cost, &$taxedCost, &$shipping, &$total)
{
    $taxedCost = '$'.round($taxedCost, 2);
    $shipping = '$'.round($shipping, 2);
    $total = '$'.round($total, 2);
    $cost = '$'.round($cost, 2);
}

Now, the variables are passed in and any changes made to them inside the function actually change them. You don't need to return anything.

By the way - your function failed because the second call (with the list command) did not pass any parameters into the function.

Also - I would read up on number_format. If you round(3,2), you get 3, not 3.00.

Sign up to request clarification or add additional context in comments.

6 Comments

I tried what you suggested but it hates my line formatDollars($cost, $taxedCost, $shipping, $total);
You have a syntax error on the first line (missing ;).
@cascaval I have the semi colon on the actual code and it still doesn't like that line
Computers do not have the capacity to "hate". If it throws an error, you must tell us what the error is. Otherwise, you just look foolish.
The error I get when I try your code , "Fatal error: Call to undefined function formatDollars() in C:\inetpub\wwwroot\BTassign4.php on line 52" and line 52 is the line I mentioned early
|
1

When followed by $ sign it may considered as variable... You may follow below code

formatDollars($cost, $taxedCost, $shipping, $total)

 function formatDollars($cost, $taxedCost, $shipping, $total) {
 setlocale(LC_MONETARY, 'en_US');
 $taxedCost = round($taxedCost, 2);
 $taxedCost =money_format('%i', $taxedCost) 
 return array( $taxedCost);
  }

Comments

0

I ended up finding the answer to my own quesion

In the line that I send variables to the function it did not like not having a variable to equal to, so when I ended up saying my variables equalled sending to the function.

function formatDollars($numberFormatted)
    {
        $numberFormat = '$'.round($numberFormatted, 2);
        return $numberFormat;
    }
    $cost = formatDollars($cost);
    $taxedCost = formatDollars($taxedCost);
    $shipping = formatDollars($shipping);
    $total = formatDollars($total);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.