prakash Posted May 28, 2010 Share Posted May 28, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/203220-php-round-help/ Share on other sites More sharing options...
Alex Posted May 28, 2010 Share Posted May 28, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/203220-php-round-help/#findComment-1064763 Share on other sites More sharing options...
prakash Posted May 28, 2010 Author Share Posted May 28, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/203220-php-round-help/#findComment-1064768 Share on other sites More sharing options...
teamatomic Posted May 28, 2010 Share Posted May 28, 2010 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. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/203220-php-round-help/#findComment-1064771 Share on other sites More sharing options...
prakash Posted May 28, 2010 Author Share Posted May 28, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/203220-php-round-help/#findComment-1064773 Share on other sites More sharing options...
Alex Posted May 28, 2010 Share Posted May 28, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/203220-php-round-help/#findComment-1064775 Share on other sites More sharing options...
prakash Posted May 28, 2010 Author Share Posted May 28, 2010 I'm analyzing poll scripts, plug-ins or mods from several cms and scripts so that I can find how they are handling this situation. Quote Link to comment https://forums.phpfreaks.com/topic/203220-php-round-help/#findComment-1064782 Share on other sites More sharing options...
prakash Posted May 28, 2010 Author Share Posted May 28, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/203220-php-round-help/#findComment-1064788 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.