How can I make a number show up as currency with 2 decimals and a comma for the thousands? In phpMyAdmin, I have a row type set up as an INT. Whenever I enter the amount, it rounds it and takes the decimals off. I tried setting it to DECIMAL, but then it adds two more decimals. So, instead of being 1,800, it comes out as 18. I tried the number format function and it worked but the cents were still rounded up and came out as 1800.00.

Here is how I did that, but I don't know if its the best way to do it because only one variable is set up for the number_format. I actually need 2 variables to have the number format.

$clientid = $row['clientid']; 
$invoice = $row['invoice']; 
$invoicedate = $row['invoicedate']; 
$utilitytype = $row['utilitytype']; 
$utilityco = $row['utilityco'];
$errortype = $row['errortype']; 
$refundamt = $row['refundamt']; 
$ourfee = $row['ourfee'];
$amount = $row['amount'];
$duedate = $row['duedate'];
$paid = $row['paid'];

$english_number_format = number_format($amount,2,'.',',');

echo "<tr>";
echo "<td>$invoice</td>";
echo "<td>$invoicedate</td>";
echo "<td>$utilitytype</td>";
echo "<td>$utilityco</td>";
echo "<td>$errortype</td>";
echo "<td>$refundamt</td>";
echo "<td>$ourfee</td>";
echo "<td>$english_number_format</td>";
echo "<td>$duedate</td>";
echo "<td>$paid</td>";
echo "</tr>";

Thanks!😃

    coco1985 wrote:

    I have a row type set up as an INT. Whenever I enter the amount, it rounds it and takes the decimals off.

    Since integers don't have a fractional/decimal amount, that shouldn't be surprising.

    I've always stored money amounts as a DECIMAL column... usually something like DECIMAL(18,2).

    coco1985 wrote:

    I tried setting it to DECIMAL, but then it adds two more decimals.

    How did you attempt to INSERT the data?

    coco1985 wrote:

    I tried the number format function and it worked but the cents were still rounded up and came out as 1800.00

    If you're referring to using the integer column, then this has nothing to do with number_format(). If the data in your DB gets truncated, then the original value is lost forever and no matter what function you use after you retrieve the data back will be based on the new truncated value.

    coco1985 wrote:

    I don't know if its the best way to do it because only one variable is set up for the number_format.

    Er... what do you mean by "set up" ? Variables don't get "set up" for anything... they simply hold values. To that end, you could change this:

    $amount = $row['amount']; 
    // ...
    $paid = $row['paid'];

    to this:

    $amount = number_format($row['amount'],2,'.',',');
    // ...
    $paid = number_format($row['paid'],2,'.',',');

    EDIT: Also, since we're talking about money, note that there is a more appropriate PHP function called [man]money_format/man for handling the formatting of the data for display purposes.

      bradgrafelman;10956809 wrote:

      Since integers don't have a fractional/decimal amount, that shouldn't be surprising.

      I've always stored money amounts as a DECIMAL column... usually something like DECIMAL(18,2).

      How did you attempt to INSERT the data?

      I inserted it in phpMYadmin in the insert tab. I think I figured out why it did that, when I insert data with a comma like 18,000 - it comes out as 18. But without the comma, it comes out fine.

      $amount = $row['amount']; 
      // ...
      $paid = $row['paid'];

      to this:

      $amount = number_format($row['amount'],2,'.',',');
      // ...
      $paid = number_format($row['paid'],2,'.',',');

      ahh that is what I needed!!!! I didn't know how to do that. Thanks!

      EDIT: Also, since we're talking about money, note that there is a more appropriate PHP function called [man]money_format/man for handling the formatting of the data for display purposes.

      I read somewhere that that function doesn't always work so I was just playing it safe with the number format.:o

        coco1985 wrote:

        when I insert data with a comma like 18,000 - it comes out as 18.

        Well that makes sense, too, since "18,000" isn't a number (unless you're from Europe and use commas instead of decimals, in which case "18.000" wouldn't be a number...)

        coco1985 wrote:

        I read somewhere that that function doesn't always work so I was just playing it safe with the number format.

        You're right - the manual page does say that Windows (as of yet) doesn't have that function. Oops - guess I'm too used to only using *nix boxes here lately. :p

          bradgrafelman;10956813 wrote:

          Well that makes sense, too, since "18,000" isn't a number (unless you're from Europe and use commas instead of decimals, in which case "18.000" wouldn't be a number...)

          You're right - the manual page does say that Windows (as of yet) doesn't have that function. Oops - guess I'm too used to only using *nix boxes here lately. :p

          haha. Thanks Mr. Smarty Pants. 😉

          Seriously, thanks, because I wouldn't have been able to make this app so fast without you. 😃

            coco1985 wrote:

            haha. Thanks Mr. Smarty Pants.

            Hey - I was just being accurate. To me, "18,000" looks like the number eighteen thousand; to a computer (set to the US locale), that looks like some data that has a non-numeric character in it.

            Don't forget to mark this thread resolved (if it is) using the link on the Thread Tools menu above.

              Write a Reply...