viva Posted January 14, 2023 Share Posted January 14, 2023 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 Quote Link to comment https://forums.phpfreaks.com/topic/315800-round-to-1-decimal/ Share on other sites More sharing options...
menator01 Posted January 14, 2023 Share Posted January 14, 2023 Have a look at round() <?php $dec = 3.2569871; echo round($dec, 1); ?> will output 3.3 Quote Link to comment https://forums.phpfreaks.com/topic/315800-round-to-1-decimal/#findComment-1604655 Share on other sites More sharing options...
Solution Barand Posted January 14, 2023 Solution Share Posted January 14, 2023 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) Quote Link to comment https://forums.phpfreaks.com/topic/315800-round-to-1-decimal/#findComment-1604657 Share on other sites More sharing options...
viva Posted January 14, 2023 Author Share Posted January 14, 2023 Thank you for your answers. Both solutions work very well but I decided to do it in the database. Quote Link to comment https://forums.phpfreaks.com/topic/315800-round-to-1-decimal/#findComment-1604663 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.