Add numberHelper support for 'e' notation

Javascript's parseFloat supports this natively, so we can handle it if
all else fails.

Test Plan:
- As a teacher, create a quiz with a numeric question
- Enter `3e4` as an answer
- On blur, see it replaced with `3000`

Closes QO-512

Change-Id: Ia7c7e196ed0baa302fc6b6f79eda611955f75d2b
Reviewed-on: https://gerrit.instructure.com/206463
Tested-by: Jenkins
Product-Review: Kevin Dougherty III <jdougherty@instructure.com>
Reviewed-by: Connor Williams <cowilliams@instructure.com>
QA-Review: David Tan <dtan@instructure.com>
This commit is contained in:
Aaron Griffin 2019-08-23 09:13:55 -05:00
parent 2378207a89
commit 8ad72a6f1c
2 changed files with 16 additions and 0 deletions

View File

@ -39,6 +39,17 @@ import I18n from 'i18nObj'
num = helper._parseNumber(input)
}
// final fallback to old parseFloat - this allows us to still support scientific 'e' notation
if (input.toString().indexOf('e') > -1 && isNaN(num)) {
num = parseFloat(input)
// Over 'e20' these don't produce usable numbers,
// so we just have to cap it for simplicity's sake...
if(num.toString().indexOf('e') > -1) {
return NaN
}
}
return num
},

View File

@ -93,6 +93,11 @@ test('returns input if already a number', () => {
equal(numberHelper.parse(input), input)
})
test('supports e notation', () => {
numberHelper._parseNumber.restore()
equal(numberHelper.parse('3e2'), 300)
})
test('parses toString value of objects', () => {
numberHelper._parseNumber.restore()
const obj = {toString: () => `2${separator}3`}