Problem in subtracting two float values in javascript
Dear Friends
function flchk()
{
var timin=parseFloat(12.10)
var timout=parseFloat(14.15)
var Tottim=(timout-timin)
alert (Tottim)
}
the above is the function I have in my web page when I subtract the two values(values are timein and time out) I get 2.05000000000000007,but i need only 2.05,please help to sort out this problem.Please note that this value is not money value it is time value, i need to get total time the employee had worked if i subtract timeout from timein
function roundAmount(n) {
var s = "" + Math.round(n * 100) / 100
var i = s.indexOf('.')
if (i < 0) return s + ".00"
var t = s.substring(0, i + 1) +
s.substring(i + 1, i + 3)
if (i + 2 == s.length) t += "0"
return t
}
function flchk(){
var timin=parseFloat(12.10);
var timout=parseFloat(14.15);
var Tottim=(timout-timin);
Tottim = roundAmount(Tottim);
alert (Tottim);
}
does this work for you:
function flchk()
{
var timin=parseFloat(12.10)
var timout=parseFloat(14.15)
var Tottim=(timout-timin)
alert (Tottim.toFixed(2))
}
try this:
function flchk()
{
var timin=parseFloat(12.10)
var timout=parseFloat(14.15)
var Tottim=(timout-timin)
//alert (Tottim.toFixed(2))
alert (Math.round(Tottim * 100) / 100)
}
at this point where there are lots comments already posted but then the asker didn't bother to comment anything on how is it already going, makes me think to look into their profile and.... surprise!
Questions Asked 5
Last 10 Grades Given C
Question Grading Record 1 Answers Graded / 1 Answers Received
function roundTooBigAFloat(n) {
var s = "" + Math.round(n * 100) / 100
var i = s.indexOf('.')
if (i < 0) return s + ".00"
var t = s.substring(0, i + 1) +
s.substring(i + 1, i + 3)
if (i + 2 == s.length) t += "0"
return t
}
function flchk(){
var timin=parseFloat(12.10);
var timout=parseFloat(14.15);
var Tottim=(timout-timin);
Tottim = roundTooBigAFloat(Tottim);
alert (Tottim);
}
50 points have been refunded here, and this question moved to the PAQ at zero points rather than to delete it, so that participants are advised of the outcome and can comment in case I need to take an adjustment action.
function roundAmount(n) {
var s = "" + Math.round(n * 100) / 100
var i = s.indexOf('.')
if (i < 0) return s + ".00"
var t = s.substring(0, i + 1) +
s.substring(i + 1, i + 3)
if (i + 2 == s.length) t += "0"
return t
}
function flchk(){
var timin=parseFloat(12.10);
var timout=parseFloat(14.15);
var Tottim=(timout-timin);
Tottim = roundAmount(Tottim);
alert (Tottim);
}