askql: toInt doesn't convert numbers to integer

πŸ¦„ 2.3:toInt
float ask(call(get('toInt'),2.3))
2.3
πŸ¦„ 

Dear Contributor,

This task has evolved a bit. Please read the comments to find out the latest description.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 15 (2 by maintainers)

Most upvoted comments

@czerwinskilukasz1

Regarding the tests;

the math dir has it’s own _tests_ dir - is it sufficient to have test coverage for toInt there, or is it also necessary to have test coverage in askscript/_tests_ ?

Great question. It’s always better to have a few more tests but in case of this resource it will be enough to just write tests in math.
The reason I asked for tests in askscript/__tests__ in a ticket with one of the operators was that an operator was attached to the resource, so I wanted the tests to verify that both the resource and an AskScript-like way of using operators work.

@czerwinskilukasz1

I can take this one as per your suggestion.

Ah, OK, understood.

OK, so the task goes as follows:

In case a non-number string or an array or an object is passed to toInt, it should throw an Error(). Otherwise, it should mimic Math.round() from Javascript.

Meaning:

  • toInt(2.3) returns 2
  • toInt('2.3') returns 2
  • toInt(2.5) returns 3
  • toInt('2.5') returns 3
  • toInt(-2.5) returns -2
  • toInt('-2.5') returns -2
  • toInt(-2.51) returns -3
  • toInt('-2.51') returns -3
  • toInt('4a') ~returns NaN~ throws an Error
  • toInt('abc') ~returns NaN~ throws an Error
  • toInt(true) returns 1
  • toInt(false) returns 0
  • toInt(null) returns 0
  • toInt([]) ~returns 0~ throws an Error
  • toInt([1]) ~returns 1~ throws an Error
  • toInt([1,2]) ~returns NaN~ throws an Error
  • toInt({a:1}) ~returns NaN~ throws an Error
  • toInt({a:1, b: 2}) ~returns NaN~ throws an Error

OK, tickets added πŸ˜„

@czerwinskilukasz1 this should round floats to be integers, I confirm that this is a bug in implementation of toInt