?
Solved

PHP help understanding the number_format()

Posted on 2003-03-02
3
Medium Priority
?
272 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

777 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