?
Solved

Problem with nubers being rounded php mysql

Posted on 2004-08-27
8
Medium Priority
?
263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 
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 2000 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

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.
Suggested Courses

765 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