ztimer Posted April 25, 2012 Share Posted April 25, 2012 Hi, Having trouble rounding the output. I would need to be sure the output is not longer than 2 spaces after comma. Any ideas? My code <script type="text/javascript"> $(document).ready(function() { $("<?php echo "#".$input_end_time; ?>").blur(function(){ var start_time = $("<?php echo "#".$input_start_time; ?>").val(); var end_time = $("<?php echo "#".$input_end_time; ?>").val(); $.getJSON("include/timedifferencecalculation.php?s="+start_time+"&e="+end_time, function(data){ var success = data.success; if (success == 1) { $("<?php echo "#".$input_difference; ?>" ).val(data.hours + " h " + data.minutes + " m "); if (data.hours + data.minutes/60 >= 6){ $("<?php echo "#".$hours_difference; ?>").val(data.hours + data.minutes/60 - 0.5); }else { $("<?php echo "#".$hours_difference; ?>").val(data.hours + data.minutes/60); } }else { $("#message").text("Viga! " + data.reason); } }) }); }); </script> This is before $("<?php echo "#".$hours_difference; ?>").val(data.hours + data.minutes/60 - 0.5); This is what im trying to get but its not working. $("<?php echo "#".$hours_difference; ?>").val( round(data.hours + data.minutes/60 - 0.5), 2 ); Quote Link to comment https://forums.phpfreaks.com/topic/261573-problem-rounding-result/ Share on other sites More sharing options...
jcanker Posted April 25, 2012 Share Posted April 25, 2012 What output are currently getting? Is the issue that you don't have control over the length? You're mixing up php inside of jQuery, and that's always been messier than I like to deal with. php and javascript handle round in different fashions. Javascript's Math.round doesn't let you specify how many digits you want after the decimal. In order to achieve that you have to use math inside your Math.round equation: var result=Math.round(original*10)/10 return includes one decimal place Quote Link to comment https://forums.phpfreaks.com/topic/261573-problem-rounding-result/#findComment-1340385 Share on other sites More sharing options...
ztimer Posted April 25, 2012 Author Share Posted April 25, 2012 Yes the problem is in lenght. I have a form that has 3 textboxes and query gets data to the first two. 1. Start time (lets say 08:30:00) 2. End time (lets say 17:20:00) 3. Duration between. (will get 8.333333333333334) I can use a simple rounding before entering the data to database but it would visually better to round it first in the textbox so the worker will not ask questions why did some numbers change and so on. I did not understand how to use the example you gave . I know a lot but sometimes feel still like a rookie in some cases. Thank you for your help. What output are currently getting? Is the issue that you don't have control over the length? You're mixing up php inside of jQuery, and that's always been messier than I like to deal with. php and javascript handle round in different fashions. Javascript's Math.round doesn't let you specify how many digits you want after the decimal. In order to achieve that you have to use math inside your Math.round equation: var result=Math.round(original*10)/10 return includes one decimal place Quote Link to comment https://forums.phpfreaks.com/topic/261573-problem-rounding-result/#findComment-1340386 Share on other sites More sharing options...
jcanker Posted April 25, 2012 Share Posted April 25, 2012 The easy fix is to not try to carry this function out inside an echo statement--just do the math, assign the result to a variable and put the variable in the echo statement. OR you should be able to break the echo quotes to do the math and then concat the rest of the echo: Note: When I copied this line to a code block I noticed the parenthesis are in the wrong place to boot--that *might* fix it, but I'd still do it this way to avoid mixing stuff up too much.. $("<?php echo "#".$hours_difference; ?>").val(". round(data.hours + data.minutes/60 - 0.5, 2)." ); You had a closing paren between .05, & 2. Quote Link to comment https://forums.phpfreaks.com/topic/261573-problem-rounding-result/#findComment-1340392 Share on other sites More sharing options...
ztimer Posted April 25, 2012 Author Share Posted April 25, 2012 Just gave it a try and this time the script does not freeze but in the result box i get not numbers but value . round(data.hours + data.minutes/60 - 0.5, 2). The easy fix is to not try to carry this function out inside an echo statement--just do the math, assign the result to a variable and put the variable in the echo statement. OR you should be able to break the echo quotes to do the math and then concat the rest of the echo: Note: When I copied this line to a code block I noticed the parenthesis are in the wrong place to boot--that *might* fix it, but I'd still do it this way to avoid mixing stuff up too much.. $("<?php echo "#".$hours_difference; ?>").val(". round(data.hours + data.minutes/60 - 0.5, 2)." ); You had a closing paren between .05, & 2. Quote Link to comment https://forums.phpfreaks.com/topic/261573-problem-rounding-result/#findComment-1340393 Share on other sites More sharing options...
Andy-H Posted April 25, 2012 Share Posted April 25, 2012 Just gave it a try and this time the script does not freeze but in the result box i get not numbers but value . round(data.hours + data.minutes/60 - 0.5, 2). The easy fix is to not try to carry this function out inside an echo statement--just do the math, assign the result to a variable and put the variable in the echo statement. OR you should be able to break the echo quotes to do the math and then concat the rest of the echo: Note: When I copied this line to a code block I noticed the parenthesis are in the wrong place to boot--that *might* fix it, but I'd still do it this way to avoid mixing stuff up too much.. $("<?php echo "#".$hours_difference; ?>").val(". round(data.hours + data.minutes/60 - 0.5, 2)." ); You had a closing paren between .05, & 2. You're using the PHP concatenate operator instead of javascript.You don't even need to concat this. $("<?php echo "#".$hours_difference; ?>").val(round(data.hours + data.minutes/60 - 0.5, 2)); Quote Link to comment https://forums.phpfreaks.com/topic/261573-problem-rounding-result/#findComment-1340394 Share on other sites More sharing options...
ztimer Posted April 25, 2012 Author Share Posted April 25, 2012 Just gave it a try and this time the script does not freeze but in the result box i get not numbers but value . round(data.hours + data.minutes/60 - 0.5, 2). The easy fix is to not try to carry this function out inside an echo statement--just do the math, assign the result to a variable and put the variable in the echo statement. OR you should be able to break the echo quotes to do the math and then concat the rest of the echo: Note: When I copied this line to a code block I noticed the parenthesis are in the wrong place to boot--that *might* fix it, but I'd still do it this way to avoid mixing stuff up too much.. $("<?php echo "#".$hours_difference; ?>").val(". round(data.hours + data.minutes/60 - 0.5, 2)." ); You had a closing paren between .05, & 2. You're using the PHP concatenate operator instead of javascript.You don't even need to concat this. $("<?php echo "#".$hours_difference; ?>").val(round(data.hours + data.minutes/60 - 0.5, 2)); Sorry but that was not helpful. Quote Link to comment https://forums.phpfreaks.com/topic/261573-problem-rounding-result/#findComment-1340398 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.