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)
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)
returns2
toInt('2.3')
returns2
toInt(2.5)
returns3
toInt('2.5')
returns3
toInt(-2.5)
returns-2
toInt('-2.5')
returns-2
toInt(-2.51)
returns-3
toInt('-2.51')
returns-3
toInt('4a')
~returnsNaN
~ throws an ErrortoInt('abc')
~returnsNaN
~ throws an ErrortoInt(true)
returns1
toInt(false)
returns0
toInt(null)
returns0
toInt([])
~returns0
~ throws an ErrortoInt([1])
~returns1
~ throws an ErrortoInt([1,2])
~returnsNaN
~ throws an ErrortoInt({a:1})
~returnsNaN
~ throws an ErrortoInt({a:1, b: 2})
~returnsNaN
~ throws an ErrorOK, tickets added π
@czerwinskilukasz1 this should round floats to be integers, I confirm that this is a bug in implementation of
toInt