Jump to content
xisto Community
XIII

PHP: How To Truncate Numbers To 4 Decimal Plaes?

Recommended Posts

i just coded a calculator for forex that calculates pivot, support and resistance points for currencies, but it calculates it and gives results with this format : xx.xxxxxxxxxxx, i need to get results to the nearest xx.xxxx so i don't need these 11 digits, just need it to give the nearest 4 digits, what should i add to my code? Edited by XIII (see edit history)

Share this post


Link to post
Share on other sites

Next time try researching the PHP manual before posing on this forum. Good luck!

 

To begin with, those people that are just biginning to learn PHP and/or are having a difficult time finding the correct function to do what they need will find very little help from the PHP manual. PHP.net is not very user friendly especially for new programmers. Searching the website usually gives you a large number of results that are unrelated to your needs. Basically, you need an experiences PHP programmer to point you to the correct function so that you can do some research. Additionally, even if the user has a little knowledge of how the PHP manual works, they may still have trouble with something like number_format(). Is it a math function or a string function. It turns out that it is a math function but could easily be categorized as a string function. For the most part, it is a string functioon. It takes the string and inserts a comma and decimal or other characters based on the user supplied arguments. It adjusts the length of the string. In fact the only tru math calculation number_format() performs is a basic round to the correct length.

 

I never send someone to PHP.net to find answers.

 

The second thing I have to say is simply this. Never ever set limits on the questions that are asked here. Never suggest that a persons question is too basic to post or that the person should have found the answer by his or herself.

 

Finally, everyone here has come to participate in the forum. Many of us are participating not only bcause we enjoy the forum but also to accrue hosting credits. One of the easiest ways to stay active in to forum is to answer other member's questions. I spend quite a bit of time monitoring the programming forums hoping to assist another member with a problem. Not only do I gain a credit or two, I get a chance to help someone out. The life of the forum relies on members starting new topics which may spawn more new topics. Perhapes now I should go and write a tutorial on the vast uses of the number_format() function in PHP.

 

XIII, the answer seec77 gave you should do the trick. I'd place it in your script just before you output the number to the user to reduce the number calculation errors caused be rounding. You may also want to keep your original 11 digit value to use for additional calculations and only display the formated number to the user.

 

Hope this helps. :(

 

vujsa

Share this post


Link to post
Share on other sites

$num = number_format($num, 4, '.', '');
This is done using the number_format function. Next time try researching the PHP manual before posing on this forum. Good luck!

 

first of all, thank you for your answer.

second, i'm learning php/mysql a time ago, i answered a lot of question about it on this forum and you can review it, never being mad at someone coz he asked an easy question to me, and if i feel that feeling, i don't reply it at all.

third, this function i forgot that's why i couldn't make it and before u answered it, somebody answered it on another forum, and he didn't say that.

final thing as vujsa said:

 

The second thing I have to say is simply this. Never ever set limits on the questions that are asked here. Never suggest that a persons question is too basic to post or that the person should have found the answer by his or herself.

 

Finally, everyone here has come to participate in the forum. Many of us are participating not only bcause we enjoy the forum but also to accrue hosting credits. One of the easiest ways to stay active in to forum is to answer other member's questions. I spend quite a bit of time monitoring the programming forums hoping to assist another member with a problem. Not only do I gain a credit or two, I get a chance to help someone out. The life of the forum relies on members starting new topics which may spawn more new topics

we're all here to help eachothers or else no usage for a forum, and if i don't ask a computer-related question on this forum, where to ask???

Some thing you shouldn't forget on this forum:

 

"Xisto ALWAYS ROCKS"

 

p.s: thank you so much vujsa

Share this post


Link to post
Share on other sites

You may also want to keep your original 11 digit value to use for additional calculations and only display the formated number to the user.

That's unfortunately very important.You should keep all your internal numbers as precise as possible, even if you display rounded values to the user. I have seen very important applications, such as payroll or tax programs, who have rounded errors, make people earn less money than should have, or paying more tax than legal, because the internal calculated values were rounded at each calculation steps, and rounding errors accumulate. Of course, 0.1% is not a very big error, but apply 0.1% to your annual salary you will see it's a big value.
So, no internal rounding, only at the final step display rounded values.

Share this post


Link to post
Share on other sites

That's unfortunately very important.

You should keep all your internal numbers as precise as possible, even if you display rounded values to the user. I have seen very important applications, such as payroll or tax programs, who have rounded errors, make people earn less money than should have, or paying more tax than legal, because the internal calculated values were rounded at each calculation steps, and rounding errors accumulate. Of course, 0.1% is not a very big error, but apply 0.1% to your annual salary you will see it's a big value.

So, no internal rounding, only at the final step display rounded values.

 


You are extremelly right for these purposes you talked about, but here i'm just coding a pivot calculator so it's not important to get the eleven numbers, the rounded numbers are not imposrtant here, because we use it in forex for analaysing, it will not hurt anyone at all :(

Share this post


Link to post
Share on other sites

i just coded a calculator for forex that calculates pivot, support and resistance points for currencies, but it calculates it and gives results with this format : xx.xxxxxxxxxxx, i need to get results to the nearest xx.xxxx so i don't need these 11 digits, just need it to give the nearest 4 digits, what should i add to my code?

 

Hi, for this you can use the round or the number_format functions. The round function will round your number to a specific precision and return a float value, instead, the number_format function returns a string value with a defined format, in this format you define the number of decimals, a string for the decimal point separator and a string for the thousands separator to use with.

 

So, the following are equivalents:

<?php$number=12.34567890123;echo "Original Number=$number<br />round function=".round($number,4) . "<br />number_format function=" . number_format($number,4);
Best regards,

Share this post


Link to post
Share on other sites

Firstly, I'm sorry for the delayed response, I kinda forgot about this thread. :D

 

XIII, I'm very sorry! I was definately not mad, but I did overreact and you should know that I am usually not such a person as I portrayed myself in that post. Actually, I just built someone on these forums a full Microsoft Excel routine, and I worked on it for quite a while and built numerous revisions of it, and I enjoyed the task at all. I noticed the tone of my original message was a bit harsh, which is why I added the pseudo-friendly "good luck" at the end. Guess it wasn't enough, so I ask for your forgiveness again.

 

vujsa, thank you for the long and considerate comment. As you will notice, my post was written almost a month ago, a small while after I first joined these forums, and I hope you will trust that I've learned the true nature of this excellent community, and have been given a lot of help by it. I've since calmed down, but your message to me still opened my eyes a bit, so thank you!

 

And again, sorry all. :unsure:

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.