• Status: Solved
• Priority: Medium
• Security: Public
• Views: 1836

# 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)
}
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

regards
Paul
0
PaulMurugesan
• 7
• 6
• 5
• +2
1 Solution

Commented:

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

Commented:
Hello Paul,

does this work for you:
function flchk()
{
var timin=parseFloat(12.10)
var timout=parseFloat(14.15)
var Tottim=(timout-timin)
}

Regards,
zvonko

0

IT ExpertCommented:
Nice routine I posted ;-)

Zvonko: toFixed is JavaScript 1.5 (IE5+ NS6+)

Michel
0

Commented:
yeah, i forgot. roundAmount function courtesy of mplungian. ;-)
0

Commented:
Hi Paul, hi Michel, hello third :-)

try this:
function flchk()
{
var timin=parseFloat(12.10)
var timout=parseFloat(14.15)
var Tottim=(timout-timin)
alert (Math.round(Tottim * 100) / 100)
}

<|;-)

0

IT ExpertCommented:
Zvonko Same result as the post from third  but without the Zero handling

Michel
0

Commented:
If for you String and Number is Same, then OK :-)

(for Zero-handling take: "00" :-)

0

Commented:
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!

these are the situations i call, "bad times"....
0

Commented:
Sorry Paul,

Math.round() is wrong in this case.
Better choice is: Math.floor()

This result to following statement:

And if you really like to have formatting, then use this: alert((Math.floor(Tottim)-Tottim)?(Math.floor(Tottim * 100) / 100):(Tottim+".00"))

Good luck,
zvonko

0

Commented:
zvonko,

you're getting yourself tired. have you tried my example? doesn't it do the formatting??
0

Commented:
This is ExpertsExchange,

and my line compares to your nine :-)

Here again in full beauty:

<|;-)

0

Author Commented:
I asked for subtracting two time values but the answer received were for subtracting two float values ,I myself found by searching the net
0

IT ExpertCommented:
It is just a name. It works the way you want

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

IT ExpertCommented:
PS: Points to third
0

IT ExpertCommented:
Zvonko: Please don't post irrelevant posts.
You just added .00 instead of handling decimals with 0 in them.

Michel
0

Commented:
Hello Paul, do you post here the solution you found yourself or do you let this question open to never end?

to Michel: why do you believe your posts are more relevant then mine?

I think your points granting is irrelevant because you offered none :-)

0

Author Commented:
sthr ="04/18/2001 13:04:00" //mm/dd/yyyy hh:min:sec
endhr="04/18/2001 13:05:00"
function timediff1(sthr,endhr)
{

date1 = new Date();
date2 = new Date();
diff  = new Date();

date1temp = new Date(sthr);
date1.setTime(date1temp.getTime());

date2temp = new Date(endhr);
date2.setTime(date2temp.getTime());

diff.setTime(Math.abs(date1.getTime() - date2.getTime()));

timediff = diff.getTime();

hours = Math.floor(timediff / (1000 * 60 * 60));

timediff -= hours * (1000 * 60 * 60);

mins = Math.floor(timediff / (1000 * 60));
timediff -= mins * (1000 * 60);

secs = Math.floor(timediff / 1000);
timediff -= secs * 1000;
var totdiff= (+ hours + "." + mins);

}
0

Author Commented:
Dear friends

Above is the answer i found mr searching the net how is it
comment after using that
0

Author Commented:
Dear friends

Above is the answer i found mr searching the net how is it
comment after using that
0

IT ExpertCommented:
That only proves it is important to ask the right question :(

We helped you change a result of a calculation from the problematic way browsers show floating point results to one with two decimals.

If you had told us you needed to show a difference between two times we would have given you something similar to what you found.
0

IT ExpertCommented:
Anyway - I think you can ask Cummunity support to delete this and return the points

Other experts opinion?

Michel
0

Commented:
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.

Thank you all.

I can then delete this when the resolution is confirmed.

Moondancer - EE Moderator
0

Commented:

Moondancer,

i'll go with Michel. kindly delete this question.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.