javascript - Jasmine toBeCloseTo what is second parameter? -


jasmine has short documentation; it's enough. not always.

i want know second parameter of tobecloseto. official reference shows:

it("the 'tobecloseto' matcher precision math comparison", function() {     var pi = 3.1415926, e = 2.78;     expect(pi).not.tobecloseto(e, 2);     expect(pi).tobecloseto(e, 0); }); 

ok precision, practically means "precision" in case? number of digits after "." should same?

my case: want compare 2 timestamps in milliseconds; if difference between them less 100, it's fine me.

as example, what's value of x in following case?

var timestamp1 = 1501254807000; var timestamp2 = 1501254807099; var timestamp3 = 1501254807100; var precision = x; expect(timestamp1).tobecloseto(timestamp2, precision); //this should pass expect(timestamp1).tobecloseto(timestamp3, precision); //this should not pass 

if precision decimal numbers, divide integers 1000 decimal numbers, anyway, don't know x. now, in way:

 expect(math.abs(timestamp2-timestamp1)).tobelessthan(100); 

but it's not readable, , use tobecloseto (since exests...).

thanks


edit. following results may help:

expect(1000000.005).tobecloseto(1000000.000,3); //fails expect(1000000.005).tobecloseto(1000000.000,2); //fails expect(1000000.005).tobecloseto(1000000.000,1); //pass 

as stated @nikolaj, "precision" parameter specifies number of decimal places matcher check precision at, rounding.

for examples you've given, first 2 assertions fail because, @ 3 , 2 decimal places respectively, numbers found different (at 2 decimal places trailing 05 becomes rounded 1). @ 1 decimal place numbers same.

the matcher accept negative precision value, e.g.:

expect(1000).tobecloseto(1003,-1); //pass expect(1000).tobecloseto(1100,-2); //fail expect(1000).tobecloseto(1100,-3); //pass 

but it's not adjustable beyond that. sadly mean matcher isn't suited purposes, you're stuck suggested solution.


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -