Reputation: 13
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
Upvotes: 0
Views: 349
Reputation: 2671
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.
Upvotes: 0
Reputation: 147166
Just add 0!
echo 10.25455 + 0;
echo 10.00000 + 0;
Output:
10.25455
10
Upvotes: 1
Reputation: 2223
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
Upvotes: 1