0

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

5 Answers5

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
0

Use decimal as a field type. Then in PHP use round(); function.

Robert
  • 19,800
  • 5
  • 55
  • 85
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
0
echo floatval($value);

will do the trick.

Brennan James
  • 332
  • 2
  • 7