【问题标题】:Numbers adding next to each other (ex. 123 + 123 = 123123)彼此相邻的数字相加(例如 123 + 123 = 123123)
【发布时间】:2021-12-15 04:56:23
【问题描述】:

所以我有一种自制经济的东西,当我添加一个工作命令并且当它工作时它会选择一个随机数和东西但是当它将数字添加到旧货币时它添加就像 ex 123+123 = 123123 应该是 246 我不知道如何解决这个问题 我尝试了所有方法,但没有任何效果

    if (message.content.toLowerCase().startsWith(prefixS.prefix + 'work')) {
    const inventoryS = await inventory.findOne({ userID: message.author.id, guildID: message.guild.id });
    if (inventoryS.item1 === 'true') {
      const payment = Math.floor(Math.random() * 125) + 25;
      inventoryS.currency += payment
      inventoryS.save()
      message.channel.send({ embeds: [new Discord.MessageEmbed().setTitle('After a long day of work your balance is').setDescription(`__Your balance__\n > Money: ${inventoryS.currency}`).setColor('BROWN')] })
    }
    }

【问题讨论】:

    标签: mongodb mongoose discord.js


    【解决方案1】:

    您的inventoryS.currency 可能是一个字符串值

    let a = '123'     //a string
    let b = 123      //an integer
    a += b
    console.log(a)   //logs 123123

    在添加之前您需要将其解析为整数

    let a = '123'
    let b = 123
    
    a = parseInt(a) + b
    console.log(a)

    更多参考here

    【讨论】:

      【解决方案2】:

      两者都必须是Numbers才能做加法,否则会加成字符串

      inventoryS.currency = parseInt(inventoryS.currency) + parseInt(payment)
      

      【讨论】:

      • 我这样做了,但它添加了更多的数字,如 123 + 123 = 257
      • nvm 现在可以工作了,谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-22
      • 2020-11-03
      相关资源
      最近更新 更多