Solved

Problem with nubers being rounded php mysql

Posted on 2004-08-27
8
244 Views
Last Modified: 2006-11-17
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') ");      
      
            
        }
0
Comment
Question by:livegirllove
  • 6
  • 2
8 Comments
 
LVL 32

Expert Comment

by:ldbkutty
ID: 11918981
>> How can I make the rate, total, and hardware fields show 2 decimal places?
In your DB or in your output Page ?
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 11918993
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') ");      
  }
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 11919004
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)
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 32

Expert Comment

by:ldbkutty
ID: 11919007
or if you dont have any problem, just give us your CREATE TABLE stmt. I'll write an ALTER table command in it.
0
 
LVL 1

Author Comment

by:livegirllove
ID: 11919023
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;
0
 
LVL 32

Accepted Solution

by:
ldbkutty earned 500 total points
ID: 11919041
>> $tax = (isset($_POST['tax'])) ? $_POST['tax'] : "";

isset() checks whether the variable is SET or NOT. The above line is not mandatory. But, it is rather safe coding to have it because you said $tax might be empty.

You have:
---------------------------------------------------------
if ($tax = "0"){
$total = (($rate * $hours) + $hardware) * $tax;
 }
else
{$total = (($rate * $hours) + $hardware);}
-----------------------------------------------------------

It is not correct. First, the if loop should contain == and not = . Second 0 is a number and not a string. you shouldnt close it with double-quotes.

>> $total = (empty($tax)) ? (($rate * $hours) + $hardware) :  (($rate * $hours) + $hardware) * $tax;

The above line is similar to:
--------------------------------

if ( empty ( $tax ) )    // empty($tax) will return TRUE if $tax is 0 or NULL or FALSE or "" (empty).
{
   $total = (($rate * $hours) + $hardware);
}
else
{
   (($rate * $hours) + $hardware) * $tax;
}

Hope you understand.
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 11919042
Sorry..in my above code else part should be like the following:

else
{
   $total = (($rate * $hours) + $hardware) * $tax;
}
0
 
LVL 1

Author Comment

by:livegirllove
ID: 11919077
thanks fo rthe explaination
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

828 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