Jump to content

round to 1 decimal


Go to solution Solved by Barand,

Recommended Posts

Hi, 

Someone helped me to create a table with data from mysql server db.

The problem is that in one column data is shown like 9016.8800048828. How to change coe the have only one decimal for this value?

Here is part of the code:

//read data of each row

while ($row=$result->fetch_assoc()) {

echo "

<tr>

<td>$row[loc_id]</td>

<td>$row[loc_name]</td>

<td>$row[description]</td>

<td>$row[Inventory]</td>

</tr>

";

}

?>

Thank you for your help 

Link to comment
https://forums.phpfreaks.com/topic/315800-round-to-1-decimal/
Share on other sites

  • Solution

There are a couple of solutions that you can apply on the MySql side.

1 ) Use mysql's ROUND() function i your query

mysql> create temporary table test1 (amount float);

mysql> insert into test1 (amount) Values (PI());

mysql> select amount, round(amount, 1) as rounded from test1;
+---------+---------+
| amount  | rounded |
+---------+---------+
| 3.14159 |     3.1 |
+---------+---------+
1 row in set (0.03 sec)

2 ) Define the column as DECIMAL instead of FLOAT

mysql> create temporary table test2 (amount DECIMAL(8,1));

mysql> insert into test2 (amount) Values (PI());

mysql> select amount from test2;
+--------+
| amount |
+--------+
|    3.1 |
+--------+
1 row in set (0.00 sec)

 

Link to comment
https://forums.phpfreaks.com/topic/315800-round-to-1-decimal/#findComment-1604657
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.