Suvasish Paul
Suvasish Paul

Reputation: 13

php only show decimal place if have fraction

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

Answers (5)

Brennan James
Brennan James

Reputation: 442

echo floatval($value);

will do the trick.

Upvotes: 0

Chukwuemeka Inya
Chukwuemeka Inya

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

Robert
Robert

Reputation: 20286

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

Upvotes: 0

Nick
Nick

Reputation: 147166

Just add 0!

echo 10.25455 + 0;
echo 10.00000 + 0;

Output:

10.25455
10

Upvotes: 1

Ketan Yekale
Ketan Yekale

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

Related Questions