I set int data type in mysql database but it not showing decimal place like 10.20 Then I set decimal(10,5) it show five decimal place with all number thought it not necessary like 10.00000. I want to show decimal places only if have fraction like 10.25455 = 10.25455 and 10 = 10 but it showing 10 = 10.00000 how to solve this
Asked
Active
Viewed 255 times
0
-
https://stackoverflow.com/a/2938322/4925008 try this one :) – Dinosan0908 Aug 28 '18 at 07:43
-
show us the php code – Shobi Aug 28 '18 at 07:43
-
1Possible duplicate of [Remove trailing zeros from decimal in SQL Server](https://stackoverflow.com/questions/2938296/remove-trailing-zeros-from-decimal-in-sql-server) – dWinder Aug 28 '18 at 07:47
5 Answers
1
You can use PHP round function.
http://php.net/manual/en/function.round.php
PHP Code:
<?php
echo round(10.25455, 5); // outputs: 10.25455
echo '<br/>';
echo round(10.00000, 5); // outputs: 10

Ketan Yekale
- 2,108
- 3
- 26
- 33
1
Just add 0!
echo 10.25455 + 0;
echo 10.00000 + 0;
Output:
10.25455
10

Nick
- 138,499
- 22
- 57
- 95
-
Nice trick :) I guess it implicitly guess the type and converts 10.25455 to int. Is that correct? – Robert Aug 28 '18 at 07:51
-
-
0
There is no datatype in mysql that supports your requirement. In fact, that is not how computers work. A number is either an int or a float.
You might want to use varchar instead, and cast to the right data type in php, but it might cause performance overhead. It is indeed a weird data type requirement. If you must go this route, consider adding an extra column for flagging data type.

Chukwuemeka Inya
- 2,575
- 1
- 17
- 23