Float values need to be rounded after 0.59, as like seconds


Float values need to be rounded after 0.59, as like seconds



Here's a script, that will add all the Float values entered in input fields. But I am getting a problem at ROUNDING THE VALUE. With my script, Value before decimal point is rounding off after 0.99(like Price of an item). but I want to round off the value after 0.59(like minutes).



When the Calculate button is clicked, the value will be displayed in another input field.




function add_number() {
var first = parseFloat(document.getElementById("sunday").value);
var second = parseFloat(document.getElementById("monday").value);
var third = parseFloat(document.getElementById("tuesday").value);
var forth = parseFloat(document.getElementById("wednesday").value);
var fifth = parseFloat(document.getElementById("thursday").value);
var sixth = parseFloat(document.getElementById("friday").value);
var seventh = parseFloat(document.getElementById("saturday").value);
var rsl = first + second + third + forth + fifth + sixth + seventh;
document.getElementById("txtresult").value = rsl.toFixed(2);
}


<input type="number" name="sunhrs" id="sunday" />
<input type="number" name="monhrs" id="monday" />
<input type="number" name="tuesday" id="tuesday" />
<input type="number" name="wedhrs" id="wednesday" />
<input type="number" name="thurshrs" id="thursday" />
<input type="number" name="frihrs" id="friday" />
<input type="number" name="sathrs" id="saturday" />

<button onclick="add_number()">Calculate</button>
<br>
<br>
<input type="number" name="txtresult" id="txtresult" />



Example: If I entered 1.50,1.50 in input fields and click on calculate button the output will be 3.00 as the value is rounded off after 0.99. but i want the value rounded after 0.59 So, that the output will be 3.40.



Thanks in advance.



EDITED: The first problem in the post (it was befaore I change); the word "result" is special for JavaScript and I have not use it for a variable name.



Secondly, forgot to add element which has an id is txtresult



;) thx your attention.





whats the value returned from the elements
– Joe Warner
Jul 2 at 19:03





If you want to do it that way you'll have to convert your 1/60 fractions to 1/100 before adding and then convert back afterwards, or split them off and add them separately in the first place. It might be easier to represent the fraction hours / minutes a different way.
– Rup
Jul 2 at 19:03






What should a value of 1.70 convert to?
– Jacob Krall
Jul 2 at 19:04


1.70





What about the addition -- should 1.5 + 1.5 equal 3.0 or 3.4?
– Jacob Krall
Jul 2 at 19:05


1.5 + 1.5


3.0


3.4





So what is the initial input value units???
– epascarello
Jul 2 at 19:06





1 Answer
1



In order to add these values together the way you want, you'll need to convert your inputs to fractional hours, and then convert back from fractional hours to the desired representation. JavaScript does not have an "hours" data structure built in. The JavaScript Number type has a decimal base, so it can only work like regular decimal numbers.


Number



e.g.


var first = hoursFromString(document.getElementById("sunday").value);
//etc.



then


document.getElementById("txtresult").value = stringFromHours(result);



If you need help writing hoursFromString or stringFromHours, please update your question with more information on the part you're having trouble with.


hoursFromString


stringFromHours





Thanks for your reply Jacob Krall. Actually what I am trying to do is like, User will enter working hours of each day as float values. if working hours of a day is 5hrs 10min then user will enter as 5.10. then all the values entered for the week need to add and show as total hours when clicked on calculate button. So that, if entered values are 1.5 and 1.5 then i need to show the total working hours as 3hrs 40min. or simply as 3.40
– charan
Jul 2 at 19:49





The value 1.5 is not 1 hour and 50 minutes, it is 1 hour and 30 minutes. This isn't an issue with rounding, it's just how Numbers work. In order to add them together, you need to convert the string input "1.50" to a decimal floating point Number that represents the fractional hours to be added, i.e. 1.8333333333333335. Double that, and you'll get 3.666666666666667. Then, at the very end, convert it back to a human-readable representation like "3hr 40min".
– Jacob Krall
Jul 2 at 19:54


1.5


Number


"1.50"


Number


1.8333333333333335


3.666666666666667


"3hr 40min"





Jacob Krall Is there is any chance to do this like, Considering the float values as "a" and "b". for example if the value is 2.40 then a=2 and b=.40 and if(b>0.59) { a=a+1; } else a; and there is no need to show in hours Jacob. just adding the values and rounding after 0.59 and showing the raw value is enough. 1.5+1.5 = 3.4 thats it.
– charan
Jul 2 at 20:13


if(b>0.59) { a=a+1; } else a;





I don't think there's any way to make that work while still using the + operator. 1.5 + 1.5 is 3.0. You could write an addHours() function that adds them for you, taking the overflow into account.
– Jacob Krall
Jul 2 at 20:15



+


1.5 + 1.5


3.0


addHours()






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?