Link to home
Start Free TrialLog in
Avatar of livegirllove
livegirllove

asked on

Problem with nubers being rounded php mysql

I am useing dollar amounts and my numbers are being rounded off.  How can I make the rate, total, and hardware fields show 2 decimal places?



        if (isset($_POST['submit']))

        {
                  echo "<meta HTTP-EQUIV=refresh content=0;url=invoice.php?view=list>";

            $clientid = ($_POST['clientid']);

            $rate = ($_POST['rate']);

            $hours = ($_POST['hours']);
               
            $hardware = ($_POST['hardware']);

            $hardwaretext = ($_POST['hardwaretext']);

            $comment = ($_POST['comment']);

                  $tax = ($_POST['tax']);

if ($tax = "0"){
$total = (($rate * $hours) + $hardware) * $tax;
 }
else
{$total = (($rate * $hours) + $hardware);}
           
                  mysql_query("INSERT INTO invoice (clientid, rate, hours, hardware, hardwaretext, comment, tax, total) VALUES ('$clientid', '$rate', '$hours', '$hardware', '$hardwaretext', '$comment', '$tax', '$total') ");      
      
            
        }
Avatar of ldbkutty
ldbkutty
Flag of India image

>> How can I make the rate, total, and hardware fields show 2 decimal places?
In your DB or in your output Page ?
Before that, you have some problem your code. Change it like the following(removed unnecessary codes + added reliable code):

if (isset($_POST['submit']))
    {
        echo "<meta HTTP-EQUIV=refresh content=0;url=invoice.php?view=list>";
        $clientid = $_POST['clientid'];
        $rate = $_POST['rate'];
        $hours = $_POST['hours'];
        $hardware = $_POST['hardware'];
        $hardwaretext = $_POST['hardwaretext'];
        $comment = $_POST['comment'];
        $tax = (isset($_POST['tax'])) ? $_POST['tax'] : "";

        // Please check whether empty() OR !empty() function in the following line.
        $total = (empty($tax)) ? (($rate * $hours) + $hardware) :  (($rate * $hours) + $hardware) * $tax;
       
        mysql_query("INSERT INTO invoice (clientid, rate, hours, hardware, hardwaretext, comment, tax, total) VALUES  
        ('$clientid', '$rate', '$hours', '$hardware', '$hardwaretext', '$comment', '$tax', '$total') ");      
  }
I think you missed out to give the length of the DECIMAL data-type as (10,2).

Just check with PHPMyAdmin ( if you have ) and change the length of the DECIMAL to (10,2)
or if you dont have any problem, just give us your CREATE TABLE stmt. I'll write an ALTER table command in it.
Avatar of livegirllove
livegirllove

ASKER

Yup the problem was my fields were 10,0  changed them to 10,2 and it works fine.

just so I understand you. Could you explain the syntax a little bit?

$tax = (isset($_POST['tax'])) ? $_POST['tax'] : "";

        // Please check whether empty() OR !empty() function in the following line.
        $total = (empty($tax)) ? (($rate * $hours) + $hardware) :  (($rate * $hours) + $hardware) * $tax;
ASKER CERTIFIED SOLUTION
Avatar of ldbkutty
ldbkutty
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry..in my above code else part should be like the following:

else
{
   $total = (($rate * $hours) + $hardware) * $tax;
}
thanks fo rthe explaination