# 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:

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>

";

}

?>

##### Share on other sites

Have a look at round()

```<?php
\$dec = 3.2569871;

echo round(\$dec, 1);
?>

will output 3.3```

##### 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)```

##### Share on other sites

Thank you for your answers.  Both solutions work very well but I decided to do it in the database.

##### 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.

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

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

×
×
×
• Create New...