attempting to better handle decimal accuracy on user input

This commit is contained in:
Alex Phillips 2022-05-26 19:57:13 -04:00
parent 0ed43c1bc5
commit edce030df6
2 changed files with 16 additions and 3 deletions

View File

@ -62,11 +62,17 @@ export default function BudgetTableAssignedCell({ budgeted, onSubmit }) {
const onKeyPress = e => {
if (e.key === 'Enter') {
try {
const newValue = Currency.inputToDinero(mexp.eval(rowValue))
const fixedValue = rowValue.replace(/\d+\.?\d*/g, (match, contents, offset, input) => {
return Currency.inputToValue(match)
})
const newValue = Currency.valueToDinero(mexp.eval(fixedValue))
setRowValue(Currency.intlFormat(newValue))
onSubmit(newValue, month)
e.target.blur()
} catch (e) {}
} catch (e) {
console.log(e)
}
}
}

View File

@ -18,7 +18,14 @@ export class Currency {
}
static inputToDinero(amount) {
return dinero({ amount: parseInt(amount * 100), currency: currencies[Currency.getCurrency()] })
return dinero({
amount: parseInt(`${parseFloat(amount).toFixed(2)}`.replace('.', '')),
currency: currencies[Currency.getCurrency()],
})
}
static inputToValue(amount) {
return dineroToValue(Currency.inputToDinero(amount))
}
static intlFormat(dineroObject, locale, options = {}) {