Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8279811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:27:01+00:00 2026-06-08T09:27:01+00:00

i have a MySQL table namely estimate_item which contains few columns that holds decimal

  • 0

i have a MySQL table namely estimate_item which contains few columns that holds decimal values with fixed precisions. here is my table structure.

tableName : estimate_item

enter image description here

while retrieving the records using PHP, i do some basic calculations using MySQL’s function, the SQL query i use is this.

SELECT 
    COUNT(ei.item_id) as total_item,
    SUM(ei.quantity) as total_quantity,
    SUM(ei.number_of_carton) as total_carton,
    SUM(ei.number_of_carton * ei.cbm_per_carton) as total_cbm,
    SUM(ei.quantity * ei.cost) as total_cost,
    SUM(ei.quantity * ei.rate) as total_rate,
    e.commission_amount,
    SUM(ei.quantity * ei.rate) + e.commission_amount as total_amount
FROM
    ai_estimate e
LEFT JOIN
    ai_estimate_item ei ON ei.estimate_id = e.id
WHERE
    ei.estimate_id = ?

for example the above query returns the result.

+------------+----------------+--------------+-----------+------------+------------+-------------------+--------------+
| total_item | total_quantity | total_carton | total_cbm | total_cost | total_rate | commission_amount | total_amount |
+------------+----------------+--------------+-----------+------------+------------+-------------------+--------------+
|          2 |            120 |          807 | 1122.6440 |     2.7500 |   137.5000 |           1500.00 |    1637.5000 |
+------------+----------------+--------------+-----------+------------+------------+-------------------+--------------+

since the following columns contains 4 precision values total_cbm_total, total_cost, total_rate, total_amount, i want to round up the values of all four columns this way.

a) if value is 1122.6440 then round it to 1122.644

b) if value is 2.7500 then round it to 2.75

c) if value is 137.5000 then round it to 137.50

d) if value is 1200.0000 then round it to 1200.00

i.e i want the values to contain minimum of two precisions, which can go up to 3 or 4 precision depending upon the values.

is there any way i could do this directly in MySQL? or the PHP way?

thanks and regards.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T09:27:02+00:00Added an answer on June 8, 2026 at 9:27 am

    If you do that in MySQL, my guess is that you’d need to overly complicate the query adding a CASE WHEN… END.

    So I’d do that in PHP.

    function reprecise($value)
    {
        $two_digit = number_format($value, 2);
        $three_digit = number_format($value, 3);
        return (float)$two_digit == (float)$three_digit ? $two_digit : $three_digit;
    }
    

    This employs a trick – first it calculates the representation with two and three digits, then checks whether these are the same floating number. If they are, then the last digit of the three-digit version must be zero and the two-digit version is used instead.

    To extend to 2, 3 or 4 digits, you could do that in a cycle:

    function reprecise($value)
    {
        $digits = 4;
        for ($rep = number_format($value, $digits--); $digits >= 2; $digits--)
        {
                $new = number_format($value, $digits);
                if ($new != $rep)
                        break;
                $rep = $new;
        }
        return $rep;
    }
    

    or, since string functions are very likely to be faster than number_format:

    function reprecise($value)
    {
        $digits = 9;
        $base   = strrev(number_format($value, $digits));
        for ($i = 0; $i < $digits-2; $i++)
                if ($base[$i] != '0')
                        break;
        return strrev(substr($base, $i));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mySQL table which contains rows based on payments. Each row contains
I have a MySQL table which contains the following two fields (There are others,
I have mysql table 'files'. which contains following order. 1. id 2.filename 3.keywords When
I have mysql table called it as R which contains many fields, but I
I have a MySQL table in which a column contains string prefixes. For instance
I have two columns in table users namely registerDate and lastVisitDate which consist of
I have a MySQL table called items that contains thousands of records. Each record
I have mysql database table for addresses, it contains separate columns for post code
I have mysql table that has a column that stores xml as a string.
I have a MySQL table that looks something like this: +-----+------------+ | id |

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.