Fix digits after decimal in input widget (#7468)

This commit is contained in:
Vicky Bansal 2021-09-22 12:58:56 +05:30 committed by GitHub
parent 8679141c76
commit 2c08417829
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -261,14 +261,22 @@ class InputComponent extends React.Component<
) {
let value = valueAsString.split(",").join("");
if (value) {
if (currentIndexOfDecimal !== indexOfDecimal) {
value = value.substr(0, currentIndexOfDecimal + fractionDigits + 1);
}
const locale = navigator.languages?.[0] || "en-US";
const formatter = new Intl.NumberFormat(locale, {
style: "decimal",
minimumFractionDigits: fractionDigits,
});
const decimalValueArray = value.split(".");
//remove extra digits after decimal point
if (
this.props.decimalsInCurrency &&
decimalValueArray[1].length > this.props.decimalsInCurrency
) {
value =
decimalValueArray[0] +
"." +
decimalValueArray[1].substr(0, this.props.decimalsInCurrency);
}
const formattedValue = formatter.format(parseFloat(value));
this.props.onValueChange(formattedValue);
} else {