Jump to content

Problem rounding result.


ztimer

Recommended Posts

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 );

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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));

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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