Danny620 Posted July 14, 2009 Share Posted July 14, 2009 basicly i have built a quiz and it gives you a percentage like Well Done You scored 2/9 Well Done You scored 22.2222222222 % but the thing is i want the percentage to be like 22% and not 22.2222222222 % heres my code $percent = $question1 + $question2 + $question3 + $question4 + $question5 + $question6 + $question7 + $question8 + $question9; $resultp = $percent/9 * 100; Quote Link to comment https://forums.phpfreaks.com/topic/165976-really-simple-but-need-help/ Share on other sites More sharing options...
.josh Posted July 14, 2009 Share Posted July 14, 2009 $resultp = (int) $percent/9 * 100; edit: well that will simply chop it off. If you want to round to nearest whole number, use round Quote Link to comment https://forums.phpfreaks.com/topic/165976-really-simple-but-need-help/#findComment-875401 Share on other sites More sharing options...
PugJr Posted July 14, 2009 Share Posted July 14, 2009 Or instead of going to the effort of what CV said, (Not the round() ) you can get the same exact thing with floor() if you want to chop off the decimal part. Pre-made functions ftw. Quote Link to comment https://forums.phpfreaks.com/topic/165976-really-simple-but-need-help/#findComment-875417 Share on other sites More sharing options...
.josh Posted July 14, 2009 Share Posted July 14, 2009 Or instead of going to the effort of what CV said, (Not the round() ) you can get the same exact thing with floor() if you want to chop off the decimal part. Pre-made functions ftw. er...I don't get it. round() is a pre-made function, too.... And casting as an int effectively does the same thing as floor(). The line of code I posted was his code that he's already using. I just inserted a cast type into it. Quote Link to comment https://forums.phpfreaks.com/topic/165976-really-simple-but-need-help/#findComment-875423 Share on other sites More sharing options...
Danny620 Posted July 14, 2009 Author Share Posted July 14, 2009 its not working one bit here my code <?php if(isset($_POST['question1'])){ $question1 = $_POST['question1']; } if(isset($_POST['question2'])){ $question2 = $_POST['question2']; } if(isset($_POST['question3'])){ $question3 = $_POST['question3']; } if(isset($_POST['question4'])){ $question4 = $_POST['question4']; } if(isset($_POST['question5'])){ $question5 = $_POST['question5']; } if(isset($_POST['question6'])){ $question6 = $_POST['question6']; } if(isset($_POST['question7'])){ $question7 = $_POST['question7']; } if(isset($_POST['question8'])){ $question8 = $_POST['question8']; } if(isset($_POST['question9'])){ $question9 = $_POST['question9']; } //results bit $user = $_SESSION['user_id']; $result = $question1 + $question2 + $question3 + $question4 + $question5 + $question6 + $question7 + $question8 + $question9; $percent = $question1 + $question2 + $question3 + $question4 + $question5 + $question6 + $question7 + $question8 + $question9; $resultp = $percent/9 * 100; floor($resultp); ?> <form name="form1" method="post" action=""> <label></label> <table width="534" height="1353" border="0" align="center"> <tr> <td height="36"> </td> <td><?php if($result){ echo "Well Done You scored $result/9" ;}?><br /><?php if($resultp){ echo "Well Done You scored $resultp %";}?></td> <td> </td> </tr> <tr> <td height="32"> </td> <td><p> </p></td> <td> </td> </tr> <tr> <td width="206" height="32">The ratio of moons to suns</td> <td width="236"><img src="images/sun.png" width="155" height="135"></td> <td width="78"><select name="question1" id="question1"> <option value="1">1:4</option> <option value="0">5:1</option> <option value="0">6:1</option> </select></td> </tr> <tr> <td height="30">The ratio of red to blue</td> <td><img src="images/shapes.png" width="155" height="135"></td> <td><select name="question2" id="question2"> <option value="0">3:7</option> <option value="0">5:1</option> <option value="1">6:2</option> </select></td> </tr> <tr> <td><table cellspacing="0" cellpadding="0" hspace="0" vspace="0" align="left"> <tr> <td valign="top" align="left"><p align="center">The ratio of blue to red</p></td> </tr> </table></td> <td><img src="images/shapes2.png" width="155" height="135"></td> <td><label></label> <select name="question3" id="question3"> <option value="0">1:4</option> <option value="1">3:5</option> <option value="0">5:3</option> </select></td> </tr> <tr> <td><table cellspacing="0" cellpadding="0" hspace="0" vspace="0" align="left"> <tr> <td valign="top" align="left"><p align="center">What ratio of blue to green?</p></td> </tr> </table></td> <td><img src="images/shapes3.png" width="155" height="135"></td> <td><select name="question4" id="question4"> <option value="0">1:7</option> <option value="1">7:1</option> <option value="0">1:8</option> </select></td> </tr> <tr> <td><table cellspacing="0" cellpadding="0" hspace="0" vspace="0" align="left"> <tr> <td valign="top" align="left"><p align="center">The ratio of blue to red</p></td> </tr> </table></td> <td><img src="images/shapes4.png" width="155" height="135"></td> <td><select name="question5" id="question5"> <option value="0">6:2</option> <option value="0">2:5</option> <option value="1">2:6</option> </select></td> </tr> <tr> <td><table cellspacing="0" cellpadding="0" hspace="0" vspace="0" align="left"> <tr> <td valign="top" align="left"><p align="center">The ratio of pink to green</p></td> </tr> </table></td> <td><img src="images/shapes5.png" width="155" height="135"></td> <td><select name="question6" id="question6"> <option value="1">3:1</option> <option value="0">1:3</option> <option value="0">3:3</option> </select></td> </tr> <tr> <td><table cellspacing="0" cellpadding="0" hspace="0" vspace="0" align="left"> <tr> <td valign="top" align="left"><p align="center">The ratio of squares to circles</p></td> </tr> </table></td> <td><img src="images/shapes6.png" width="155" height="135"></td> <td><select name="question7" id="question7"> <option value="0">2:3</option> <option value="1">1:3</option> <option value="0">3:1</option> </select></td> </tr> <tr> <td>What ratio of green to blue?</td> <td><img src="images/shapes7.png" width="155" height="135"></td> <td><select name="question8" id="question8"> <option value="0">2:7</option> <option value="1">2:6</option> <option value="0">6:2</option> </select></td> </tr> <tr> <td><table cellspacing="0" cellpadding="0" hspace="0" vspace="0" align="left"> <tr> <td valign="top" align="left"><p align="center">The ratio of legs to tails</p></td> </tr> </table></td> <td><img src="images/shapes8.png" width="155" height="135"></td> <td><select name="question9" id="question9"> <option value="0">4:2</option> <option value="0">1:4</option> <option value="1">4:1</option> </select></td> </tr> <tr> <td> </td> <td><input type="submit" name="button" id="button" value="Submit"></td> <td> </td> </tr> </table> <p> <label></label> </p> <p> <label></label> </p> </form> Quote Link to comment https://forums.phpfreaks.com/topic/165976-really-simple-but-need-help/#findComment-875431 Share on other sites More sharing options...
.josh Posted July 14, 2009 Share Posted July 14, 2009 That's because you didn't assign it to anything. $resultp = floor($percent/9 * 100); Quote Link to comment https://forums.phpfreaks.com/topic/165976-really-simple-but-need-help/#findComment-875435 Share on other sites More sharing options...
Danny620 Posted July 14, 2009 Author Share Posted July 14, 2009 oh lol thank you Quote Link to comment https://forums.phpfreaks.com/topic/165976-really-simple-but-need-help/#findComment-875437 Share on other sites More sharing options...
Daniel0 Posted July 14, 2009 Share Posted July 14, 2009 And casting as an int effectively does the same thing as floor(). Nope... truncation rounds towards 0 where as flooring rounds towards -∞. Quote Link to comment https://forums.phpfreaks.com/topic/165976-really-simple-but-need-help/#findComment-875441 Share on other sites More sharing options...
.josh Posted July 14, 2009 Share Posted July 14, 2009 And casting as an int effectively does the same thing as floor(). Nope... truncation rounds towards 0 where as flooring rounds towards -∞. Further explain? If I have for example 5.5 if I were to cast it as an int it would chop off the .5 and make it 5. If I have 5.5 and do floor(5.5) it rounds down to nearest integer, making it 5 just the same. So if I have for ($x = 0; $x < 5; $x += .1) { $a = (int) $x; $b = floor($x); echo "$a $b <br/>"; } All the numbers would match up: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 That makes them effectively the same thing. I mean, I get what you're saying; if you do for instance floor(-5.5) you will get -6, whereas (int) -5.5 would yield -5. But that's not applicable in this situation. Quote Link to comment https://forums.phpfreaks.com/topic/165976-really-simple-but-need-help/#findComment-875457 Share on other sites More sharing options...
Daniel0 Posted July 14, 2009 Share Posted July 14, 2009 That's because 0 and -∞ are in the same direction for positive real numbers. You said the two operations were identical, but it's not true. It's also not true in the sense that int type casting returns an int (obviously), but floor() returns a float. 1 !== 1.0. Quote Link to comment https://forums.phpfreaks.com/topic/165976-really-simple-but-need-help/#findComment-875461 Share on other sites More sharing options...
.josh Posted July 14, 2009 Share Posted July 14, 2009 I understand that they are not the same thing and in a broader scope they will not yield the same results, but I said they effectively do the same thing, and that's because in this context (for the OP's needs) they will give him what he needs all the same. Quote Link to comment https://forums.phpfreaks.com/topic/165976-really-simple-but-need-help/#findComment-875465 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.