Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions test/ui/number-input-precision.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const should = require('should') // eslint-disable-line no-unused-vars

const { stepPrecision } = require('../../ui/src/widgets/ui-number-input/precision.js')

describe('ui-number-input precision', function () {
const cases = [
[1e-7, 7],
[-1.25e-7, 9],
[0.25, 2],
[1, 0],
[0, 0],
[1e21, 0],
[1e-100, 100]
]

for (const [step, expected] of cases) {
it(`uses ${expected} decimal places for ${step}`, function () {
stepPrecision(step).should.equal(expected)
})
}

it('preserves a small step when formatted with toFixed', function () {
const step = 1e-7
step.toFixed(stepPrecision(step)).should.equal('0.0000001')
})
})
5 changes: 3 additions & 2 deletions ui/src/widgets/ui-number-input/UINumberInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import DOMPurify from 'dompurify'
import { VNumberInput } from 'vuetify/lib/components/VNumberInput/VNumberInput'
import { mapState } from 'vuex' // eslint-disable-line import/order

import { stepPrecision } from './precision.js'

export default {
name: 'DBUITextInput',
components: {
Expand Down Expand Up @@ -103,8 +105,7 @@ export default {
return Math.abs(this.getProperty('step')) || 1
},
precision () {
const s = Math.abs(this.getProperty('step')) || 1
return s.toString().split('.')[1]?.length || 0
return stepPrecision(this.step)
},
spinner () {
return this.getProperty('spinner')
Expand Down
13 changes: 13 additions & 0 deletions ui/src/widgets/ui-number-input/precision.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function stepPrecision (step) {
const [coefficient, exponent = '0'] = step.toString().split('e')
const fractionDigits = coefficient.split('.')[1]?.length || 0
// VNumberInput uses toFixed(), which accepts at most 100 digits.
return Math.min(100, Math.max(0, fractionDigits - Number(exponent)))
}

export { stepPrecision }

if (typeof module !== 'undefined' && module.exports) {
module.exports = { stepPrecision }
module.exports.default = module.exports
}
Loading