Nov 8, 2009

PHP: How To Truncate Numbers To 4 Decimal Plaes?

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Scripting > PHP

PHP: How To Truncate Numbers To 4 Decimal Plaes?

XIII
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?

Comment/Reply (w/o sign-up)

seec77
CODE

$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!

Comment/Reply (w/o sign-up)

vujsa
QUOTE(seec77 @ Jun 18 2006, 09:39 AM) *

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. cool.gif

vujsa

 

 

 


Comment/Reply (w/o sign-up)

XIII
QUOTE(seec77 @ Jun 18 2006, 10:39 PM) *

CODE

$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:

QUOTE
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:

"ASTAHOST ALWAYS ROCKS"

p.s: thank you so much vujsa


Comment/Reply (w/o sign-up)

yordan
QUOTE
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.

Comment/Reply (w/o sign-up)

XIII
QUOTE(yordan @ Jun 21 2006, 11:18 PM) *

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 smile.gif

Comment/Reply (w/o sign-up)

TavoxPeru
QUOTE(XIII @ Jun 17 2006, 06:46 PM) *

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:
CODE
<?php
$number=12.34567890123;
echo "Original Number=$number<br />round function=".round($number,4) . "<br />number_format function=" . number_format($number,4);

Best regards,

Comment/Reply (w/o sign-up)

seec77
Firstly, I'm sorry for the delayed response, I kinda forgot about this thread. tongue.gif

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. biggrin.gif

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)


See Also,

*SIMILAR VIDEOS*
Searching Video's for calculation, problem
advertisement



PHP: How To Truncate Numbers To 4 Decimal Plaes?

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com