Formating numbers

Posted on 2006-04-27
Last Modified: 2013-12-12

is there a way to format number without using format_number I'm using fpdf to output some graph and if I use number_format it get confused. I do not know why but it dose.

I only need to format the number for printing 87.12345678 just wont cut it I need to display 87 or 12345.12345678 just wont do I need to display 12,345 is there a way to acomplish this?

my out put goes from 87 dollars to as high as 78,564.


Question by:rcleon

    Expert Comment

    Well, I'm not sure why it would be choking on number_format.  That seems to be the best route to go and if you don't want decimals do you want to round the number?

     If so try...
    number_format(round($int, 0));

    There may be a better solution but I've never had problems with number_format before.. What exact problems did you see with the number_format?
    LVL 40

    Accepted Solution

    THe only reason I can think of is that you are formatting numbers (1234.56 => 1,234.56) to use in a chart and the data is ...

    1234.56,2345.67 which now becomes ...


    The commas in the numbers in the data being sent to the chart are causing confusion. Maybe.

    If you DON'T format the numbers, does the chart work (probably yes), but the chart also shows the numbers?

    Are the numbers you want to see formatted the data or the axis?
    LVL 40

    Expert Comment

    And to format a number with no decimal part ...

    number_format($value, 0);

    Rounding like 8ball629 suggested, is probably a not a good idea, but that depends upon the data and what the chart is showing.
    LVL 11

    Expert Comment

    if you are retrieving the values from database, you can try formatting from the database.
    For example for MySQL
    select format(12345.78,0)

    would return 12,346


    Author Comment

    I think you guys are right it is the commas and/or the decimal here is the portion of the calc.

    function BarDiagram($w, $h, $data, $format, $color=null, $maxVal=0, $nbDiv=4)
                $this->SetFont('Arial', 'B', 10);

                $XPage = $this->GetX();
                $YPage = $this->GetY();
                $margin = 2;
                $YDiag = $YPage + $margin;
                $hDiag = floor($h - $margin * 2);
                $XDiag = $XPage + $margin * 2 + $this->wLegend;
                $lDiag = floor($w - $margin * 3 - $this->wLegend);
                if($color == null)

                if ($maxVal == 0) {
                      $maxVal = max($data);

                $valIndRepere = ceil($maxVal / $nbDiv);
                $maxVal = $valIndRepere * $nbDiv;
                $lRepere = floor($lDiag / $nbDiv);
                $lDiag = $lRepere * $nbDiv;
                $unit = $lDiag / $maxVal;
                $hBar = floor($hDiag / ($this->NbVal + 1));
                $hDiag = $hBar * ($this->NbVal + 1);
                $eBaton = floor($hBar * 80 / 100);

    Any help will be appricated.


    I send more that what I think you would need but I wanted to make sure you could see the parameters of the function.

    Author Comment

    Thank you guys,

    I took the hint from RQ an was able to rewrite the function taking into consideration the comma. If this had not been suggested I would have never found the solution.



    Expert Comment

    For the record, the reason I was suggesting round it is because of the following:

    I only need to format the number for printing 87.12345678 just wont cut it I need to display 87 or 12345.12345678 just wont do I need to display 12,345 is there a way to acomplish this?

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    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…
    The viewer will learn how to dynamically set the form action using jQuery.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    733 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

    23 Experts available now in Live!

    Get 1:1 Help Now