Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

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']);?>
share|improve this question
    
I think this question is more suitable for stackoverflow.com –  mustaccio Feb 12 at 19:45
    
Stackoverflow doesn't allow me to post question even though I have 70+ reputation and many badges –  Khan Shahrukh Feb 12 at 20:08
    
What datatype are you using to store the numbers? –  Colin 't Hart Feb 12 at 22:44
    
I am using text as data type –  Khan Shahrukh Feb 13 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 at 7:07

1 Answer 1

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)

mysql>

Give it a Try !!!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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