Solved

Problem with nubers being rounded php mysql

Posted on 2004-08-27
8
220 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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
 
LVL 32

Expert Comment

by:ldbkutty
Comment Utility
or if you dont have any problem, just give us your CREATE TABLE stmt. I'll write an ALTER table command in it.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:livegirllove
Comment Utility
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
Comment Utility
>> $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
Comment Utility
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
Comment Utility
thanks fo rthe explaination
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

728 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

10 Experts available now in Live!

Get 1:1 Help Now