Solved

# Problem in subtracting two float values in javascript

Posted on 2002-04-17
1,423 Views
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
Question by:PaulMurugesan
• 7
• 6
• 5
• +2

LVL 30

Expert Comment

ID: 6949660

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

LVL 10

Expert Comment

ID: 6949717
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

LVL 75

Expert Comment

ID: 6949722
Nice routine I posted ;-)

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

Michel
0

LVL 30

Expert Comment

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

LVL 10

Expert Comment

ID: 6949751
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

LVL 75

Expert Comment

ID: 6949788
Zvonko Same result as the post from third  but without the Zero handling

Michel
0

LVL 10

Expert Comment

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

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

0

LVL 30

Expert Comment

ID: 6949838
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

LVL 10

Expert Comment

ID: 6949879
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

LVL 30

Expert Comment

ID: 6949912
zvonko,

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

LVL 10

Expert Comment

ID: 6949929
This is ExpertsExchange,

and my line compares to your nine :-)

Here again in full beauty:

<|;-)

0

Author Comment

ID: 6950068
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

LVL 75

Expert Comment

ID: 6950107
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

LVL 75

Expert Comment

ID: 6950109
PS: Points to third
0

LVL 75

Expert Comment

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

Michel
0

LVL 10

Expert Comment

ID: 6950353
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 Comment

ID: 6952807
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 Comment

ID: 6952821
Dear friends

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

Author Comment

ID: 6952907
Dear friends

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

LVL 75

Expert Comment

ID: 6953016
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

LVL 75

Expert Comment

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

Other experts opinion?

Michel
0

LVL 1

Accepted Solution

Moondancer earned 0 total points
ID: 6953731
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

LVL 30

Expert Comment

ID: 6958819

Moondancer,

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

## Featured Post

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be \$37.1B.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQueryâ€¦