[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Problem with nubers being rounded php mysql

Posted on 2004-08-27
8
Medium Priority
?
270 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

649 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