I am fetching data from MySQL using PHP. I know that most values stored in the database are float. Sometimes, the database outputs values like 3.9999E-5 and sometimes like 0.00104961904762 (14 digits).

How can I truncate the value to 8 digits only. I am currently using:

<? echo sprintf("%.8f",$get_results['Price']);?>
  • I think this question is more suitable for stackoverflow.com – mustaccio Feb 12 '14 at 19:45
  • Stackoverflow doesn't allow me to post question even though I have 70+ reputation and many badges – Khan Shahrukh Feb 12 '14 at 20:08
  • What datatype are you using to store the numbers? – Colin 't Hart Feb 12 '14 at 22:44
  • I am using text as data type – Khan Shahrukh Feb 13 '14 at 3:48
  • You have already posted two questions at stackoverflow. So why do you think that you are not allowd to post questions at stackoverflow? – miracle173 Mar 15 '14 at 7:07

You need MySQL's FORMAT function

To round 0.00000123456 to 8 places, run the following within PHP:

mysql> select FORMAT(0.00000123456,8);
| FORMAT(0.00000123456,8) |
| 0.00000123              |
1 row in set (0.00 sec)


Give it a Try !!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.