[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1689
  • Last Modified:

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

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

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);
}
0
 
zvonkoCommented:
Hello Paul,

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

Regards,
zvonko

0
 
Michel PlungjanIT ExpertCommented:
Nice routine I posted ;-)

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

Michel
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
thirdCommented:
yeah, i forgot. roundAmount function courtesy of mplungian. ;-)
0
 
zvonkoCommented:
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 (Tottim.toFixed(2))
    alert (Math.round(Tottim * 100) / 100)
    }

<|;-)

0
 
Michel PlungjanIT ExpertCommented:
Zvonko Same result as the post from third  but without the Zero handling

Michel
0
 
zvonkoCommented:
If for you String and Number is Same, then OK :-)

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

0
 
thirdCommented:
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

these are the situations i call, "bad times"....
0
 
zvonkoCommented:
Sorry Paul,

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

This result to following statement:
 alert(Math.floor(Tottim * 100) / 100)

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
 
thirdCommented:
zvonko,

   you're getting yourself tired. have you tried my example? doesn't it do the formatting??
0
 
zvonkoCommented:
This is ExpertsExchange,

and my line compares to your nine :-)

Here again in full beauty:
alert((Math.floor(Tottim)-Tottim)?(Math.floor(Tottim * 100) / 100):(Tottim+".00"))

<|;-)

0
 
PaulMurugesanAuthor 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
 
Michel PlungjanIT 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);
 alert (Tottim);
}
0
 
Michel PlungjanIT ExpertCommented:
PS: Points to third
0
 
Michel PlungjanIT ExpertCommented:
Zvonko: Please don't post irrelevant posts.
You just added .00 instead of handling decimals with 0 in them.

Michel
0
 
zvonkoCommented:
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
 
PaulMurugesanAuthor 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);
return totdiff;

}
0
 
PaulMurugesanAuthor Commented:
Dear friends

Above is the answer i found mr searching the net how is it
comment after using that
0
 
PaulMurugesanAuthor Commented:
Dear friends

Above is the answer i found mr searching the net how is it
comment after using that
0
 
Michel PlungjanIT ExpertCommented:
That only proves it is important to ask the right question :(

Your original question was not worded in such a way that we could help you proper.

"I get 2.05000000000000007,but i need only 2.05,please help to sort out this problem"

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
 
Michel PlungjanIT ExpertCommented:
Anyway - I think you can ask Cummunity support to delete this and return the points

Other experts opinion?

Michel
0
 
MoondancerCommented:
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.

zvonko, for you please. http://www.experts-exchange.com/commspt/Q.20290918.html

I can then delete this when the resolution is confirmed.

Moondancer - EE Moderator
0
 
thirdCommented:

Moondancer,

i'll go with Michel. kindly delete this question.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 7
  • 6
  • 5
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now