如何使用拆分正确获得值?

发布于 2025-02-08 15:29:31 字数 1491 浏览 1 评论 0原文

我正在尝试使用户输入的值,每当他更改此兑换价值时,也会更改,但是当我放置一个较小的值时:r $ 5.00 什么都没有发生, 仅接受高价值:400.00和返回荒谬的40,000.00。 当我尝试放置较小的值时,以下NAN错误会发生

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>

掩码


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';
};

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';
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文