bachx Posted August 20, 2007 Share Posted August 20, 2007 This is more of a mathmatical question but if anyone knows, I'd really appreciate it. Say I have two numbers, for example 150 and 450....and a third number between them, say 250. I want to calculate the percentage of 250 between 150 and 450, where 150 is the 0%, and 450 is the 100%. I know there is a certain formula, but seeing math was never my favorite course, I can't just figure it out. Quote Link to comment https://forums.phpfreaks.com/topic/65783-solved-percentages/ Share on other sites More sharing options...
MadTechie Posted August 20, 2007 Share Posted August 20, 2007 $start = 150; $end = 450; $cValue = 250; $currentPercent = $cValue * ($start/ $end); Opps, didn't read the end of the question $currentPercent = $cValue * (100 / ($end - $start)) Quote Link to comment https://forums.phpfreaks.com/topic/65783-solved-percentages/#findComment-328653 Share on other sites More sharing options...
bachx Posted August 20, 2007 Author Share Posted August 20, 2007 Hmm, something is wrong in that formula. It sometimes gives values above 100% Example: $start = 50; $end = 150; $cValue = 125; The result will be 125% Quote Link to comment https://forums.phpfreaks.com/topic/65783-solved-percentages/#findComment-328661 Share on other sites More sharing options...
vijayfreaks Posted August 20, 2007 Share Posted August 20, 2007 Hi... <?php $no1=150; $no2=450; $diff_no=($no2-$no1)/100; $test_no=150; $j=1; for ($i=$no1;$i<=$no2;$i+=$diff_no) { if($i>=$test_no) { break; } $j++; } $per=$j-1; echo "my test: ".$per."%"; ?> Regards, Vijay Quote Link to comment https://forums.phpfreaks.com/topic/65783-solved-percentages/#findComment-328662 Share on other sites More sharing options...
AndyB Posted August 20, 2007 Share Posted August 20, 2007 Huge sigh: $whole_thing = $high_num - $low_num; // the range, or 100%, e.g. 450-150=300 $part_thing = $some_number - $low_num; // the unknown bit of the range, e.g. 250-150=100 $percent = 100 * $part_thing / $whole_thing;// .g. 100*100/300 = 33.33% Quote Link to comment https://forums.phpfreaks.com/topic/65783-solved-percentages/#findComment-328668 Share on other sites More sharing options...
bachx Posted August 20, 2007 Author Share Posted August 20, 2007 Thanks vijayfreaks, that worked. But I'm not sure if looping 300 times to display the percentage is efficient. What if I had much larger numbers? Is there any more efficient approach than this? Quote Link to comment https://forums.phpfreaks.com/topic/65783-solved-percentages/#findComment-328669 Share on other sites More sharing options...
AndyB Posted August 20, 2007 Share Posted August 20, 2007 Thanks vijayfreaks, that worked. But I'm not sure if looping 300 times to display the percentage is efficient. What if I had much larger numbers? Is there any more efficient approach than this? PLEASE - read my post. Quote Link to comment https://forums.phpfreaks.com/topic/65783-solved-percentages/#findComment-328672 Share on other sites More sharing options...
bachx Posted August 20, 2007 Author Share Posted August 20, 2007 Huge sigh: $whole_thing = $high_num - $low_num; // the range, or 100%, e.g. 450-150=300 $part_thing = $some_number - $low_num; // the unknown bit of the range, e.g. 250-150=100 $percent = 100 * $part_thing / $whole_thing;// .g. 100*100/300 = 33.33% Worked like a charm. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/65783-solved-percentages/#findComment-328676 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.