PHP Number Format - Decimal Places

Posted on 2007-10-19
Last Modified: 2008-01-09

I have this line in my PHP code that displays a decimal number, but sometimes there is like 10 decimal places. I need to shorten it to display only 2 decimal places.

For example, now:

It will display: 69.9438095238

I need it to display like: 69.94

Here is the line of PHP code:


echo ('<td class=' . $css_class . ' align=default >'.$_SESSION["avg"].'</td>');


How can I make it display with only 2 decimal places?

Question by:fcruz5
    LVL 24

    Accepted Solution

    echo ('<td class=' . $css_class . ' align=default >'.round($_SESSION["avg"], 2).'</td>');
    LVL 2

    Expert Comment

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

    Expert Comment

    Its good to use the round($value, 2) function first and then wrap it with number_format($value, 2).
    It should look like $number_format(round($value, 2), 2)

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    732 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now