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
https://forums.phpfreaks.com/topic/203220-php-round-help/
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
https://forums.phpfreaks.com/topic/203220-php-round-help/#findComment-1064763
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
https://forums.phpfreaks.com/topic/203220-php-round-help/#findComment-1064768
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
https://forums.phpfreaks.com/topic/203220-php-round-help/#findComment-1064773
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
https://forums.phpfreaks.com/topic/203220-php-round-help/#findComment-1064775
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
https://forums.phpfreaks.com/topic/203220-php-round-help/#findComment-1064788
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.