首页 > 解决方案 > 为什么我的变量会发生变化?

问题描述

从另一个脚本调用函数时,我的代码出现了一些问题。当我通过变量发送数据时,添加一个 1。是否可以从最后 2 位数字中获取值并从中删除 1,然后再次将它们合并到主 int 中?

我在哪里调用函数:(test.js):

cards.getcards(76561198089544929, function(err, res, body){});

在具有功能(index.js)的脚本内部:

exports.getcards = function(steamid, callback){
console.log(steamid);
}

输出:76561198089544930

使固定

        var steamid = 76561198089544930; //This is what I got from the function.
        var toText = steamid.toString(); //convert to string
        var lastChar = toText.slice(-2); //gets last character
        var baseChars = toText.slice(0, -2);
        var lastDigit = +(lastChar); //convert last character to number
        var newlast = lastDigit - 1;
        var steamid = baseChars+newlast;
        alert(steamid);

标签: javascript

解决方案


该数字太大而无法放入 javascript Number 对象中。你在这里有三个选择。要么使用字符串来存储数字,要么使用库来获取任意精度的数字,或者只是让数字更小。


推荐阅读