Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

PHP help understanding the number_format()

Posted on 2003-03-02
3
Medium Priority
?
288 Views
Last Modified: 2008-03-03
Hi

I am just learning PHP and I can't understand why formating the same number twice gives a different result

I.E. the code below returns

Total including tax: $1,000.50
Total including tax: $1.00

Maybe someone could shed some light on this so I understand it better


<?
$totalamount = 1000.50;
 $totalamount = number_format($totalamount, 2);
  echo "Total including tax: $".$totalamount."<br>\n";
  $totalamount = number_format($totalamount, 2);
    echo "Total including tax: $".$totalamount."<br>\n";
?>

0
Comment
Question by:profess
  • 2
3 Comments
 
LVL 2

Expert Comment

by:dboeke
ID: 8054077
If you check out the php manual:

http://www.php.net/manual/en/function.number-format.php

you will see that the function number_format() has the following signature:

string number_format ( float number [, int decimals [, string dec_point [, string thousands_sep]]])

The function requires a floating-point number (float) as the first parameter, and you also supplied the number of decimal places (2) which is an integer (int) as the second parameter.

The function returns a String as its output.

The first time that you run execute the function, your code looks like this:

$totalamount = 1000.50;
$totalamount = number_format($totalamount, 2);

the second line is equivalent to:  $totalamount = number_format(1000.50, 2);

when this line is finished executing, you have changed $totalamount to a String $total amount is now equal to “1,000.50”

Now, the second time you execute the command, your code looks like this:

$totalamount = number_format($totalamount, 2);

This line is equivalent to writing:  $totalamount = number_format(“1,000.50”, 2);

Because the number_format function requires a float as the first parameter, and you supplied a string, php first converts your string to a float. Since the comma is a valid decimal separator in Europe, the floating-point number it converts to is 1.000.  So logically, your statement is equivalent to:  $totalamount = number_format(1.000, 2);

To solve the problem, don’t mix variable types, keep strings strings, and numbers numbers:

<?
$fltTotalAmount = 1000.50;
$strTotalAmount = number_format($fltTotalAmount, 2);
 echo "Total including tax: $".$strTotalAmount."<br>\n";
 $strTotalAmount = number_format($fltTotalAmount, 2);
   echo "Total including tax: $".$strTotalAmount."<br>\n";
?>

0
 
LVL 2

Accepted Solution

by:
dboeke earned 200 total points
ID: 8054097
Because I copied and pasted, it changed my smart (or maybe they should be dumb) quotes to boxes like this:  ““““

The middle part of my answer should look like this:

When this line is finished executing, you have changed $totalamount to a String $total amount is now equal to "1,000.50"

Now, the second time you execute the command, your code looks like this:

$totalamount = number_format($totalamount, 2);

this line is equivalent to writing:  $totalamount = number_format("1,000.50", 2);
0
 

Author Comment

by:profess
ID: 8054196
Thankyou, I had just accidently number_formated the same var twice the code was much larger and it was quite difficult to find....that is why I wanted to make sure I understood it.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question