I'm trying to get the value entered by the user and whenever he changes this redeemed value is also changed, but when I put a small value: R$5.00 nothing happens, is only accepting high value: 400.00 and the return an absurd 40,000.00. When I try to put a small value, the following NaN error occurs
const {valueAmount, textDescription} = props.navigation.state.params;
const amoutSplit = valueAmount.split('R$ ');
const includComma = amoutSplit[1].split(',');
const amout = Number(includComma[0] + includComma[1]);
<Text>{Filters.convertMoneyTextMask(amout)}
</Text>
the mask
const convertMoneyTextMask = (value) => {
if (value) {
const stringOnlyNumbers = `${Number(value).toFixed(2)}`.replace(/\D/g, '');
if (!stringOnlyNumbers) {
return 'R$ 0,00';
}
const {length} = stringOnlyNumbers;
if (length === 1) {
return value >= 0
? `R$ 0,0${stringOnlyNumbers}`
: `R$ -0,0${stringOnlyNumbers}`;
}
if (length === 2) {
return value >= 0
? `R$ 0,${stringOnlyNumbers}`
: `R$ -0,${stringOnlyNumbers}`;
}
let moneyMask = '';
for (let i = length - 1; i >= 0; i -= 1) {
if (i === length - 2) {
moneyMask = `,${stringOnlyNumbers[i]}${moneyMask}`;
} else if (i < length - 5 && (i - length - 3) % 3 === 0) {
moneyMask = `${stringOnlyNumbers[i]}.${moneyMask}`;
} else {
moneyMask = `${stringOnlyNumbers[i]}${moneyMask}`;
}
}
return value >= 0 ? `R$ ${moneyMask}` : `R$ -${moneyMask}`;
}
return 'R$ 0,00';
};