0

The number_format function in PHP seems to round by default. My understanding is that this function is useful for separating every three numbers with a comma. Like 1403423 becomes 1,404,423 when using number_format.

So, if I have a large number that I want rounded to two decimal places, how can I do this and still have the commas properly displayed?

Desired behavior: 12042.529 --> 12,042.53

3
  • Um.... where? php.net/manual/en/function.number-format.php Commented Sep 26, 2013 at 4:34
  • 1
    If only one parameter is given, number will be formatted without decimals, but with a comma (",") between every group of thousands. If two parameters are given, number will be formatted with decimals decimals with a dot (".") in front, and a comma (",") between every group of thousands. Commented Sep 26, 2013 at 4:36
  • None of that talks about rounding. Commented Sep 26, 2013 at 4:39

1 Answer 1

1
$a=12042.529;
echo number_format($a,2,'.',',');
Sign up to request clarification or add additional context in comments.

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.