Jump to content

php round help


prakash

Recommended Posts

Hi,

 

I had made a php poll script, and I am having an issue on the page where I display the individual options percentage.

I use php round() function and when I count the each option's vote percentage (used round on each options while calculating) I didn't get 100% in total.

 

Like I have following data:

Opt 1: 425 - 61% (with php round)

Opt 2: 176 - 25%  (with php round)

Opt 3: 92  - 13%  (with php round)

===============

Total: 693  - 99%

 

So where does 1% goes here?

what will be the correct way to calculate percentage?

Link to comment
Share on other sites

There's nothing necessarily wrong with that.. You're rounding, by definition this means it won't be as accurate. Look at the numbers:

 

1 : 61.3275613..% becomes 61% (loss of .3275613..%)

2 : 25.3968254..% becomes 25% (loss of .3968254..%)

3 : 13.2756133..% becomes 12% (loss of .2756133..%)

 

If you want it to be more accurate you can have it round to some decimal places. Ex:

 

echo round(65.34654, 2); // 65.35

Link to comment
Share on other sites

There's nothing necessarily wrong with that.. You're rounding, by definition this means it won't be as accurate. Look at the numbers:

 

1 : 61.3275613..% becomes 61% (loss of .3275613..%)

2 : 25.3968254..% becomes 25% (loss of .3968254..%)

3 : 13.2756133..% becomes 12% (loss of .2756133..%)

 

If you want it to be more accurate you can have it round to some decimal places. Ex:

 

echo round(65.34654, 2); // 65.35

 

isn't it possible using other methods? I don't like to show decimals on percentages.

Link to comment
Share on other sites

Round will round up or down depending on the decimal. If you want 100% you can fudge it and if its 99 add 1 to the lowest, if its 101 subtract 1 from the highest.

Teamatomic

 

I have no plan to display the total percentage. If I display the individual percentage in such way (61%, 25%, 13%) which totals 99% then, the users impression on the system become pretty bad. Isn't it?

Link to comment
Share on other sites

Any method that you use to make them seem to come out to 100% would only make things even less accurate and as a result make the system look even worse. Look around to see how other popular applications handle this problem. In most cases I think you'll find that they either show some decimals (SMF polls for example) or just leave it as is, I don't really think it's a big deal.

Link to comment
Share on other sites

Any method that you use to make them seem to come out to 100% would only make things even less accurate and as a result make the system look even worse. Look around to see how other popular applications handle this problem. In most cases I think you'll find that they either show some decimals (SMF polls for example) or just leave it as is, I don't really think it's a big deal.

 

yes we need to make correction customly by getting buffer error and correct it by decreasing that error percentage from one of the options.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.