javascript - 为什么我的变量会发生变化?
问题描述
从另一个脚本调用函数时,我的代码出现了一些问题。当我通过变量发送数据时,添加一个 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 Number 对象中。你在这里有三个选择。要么使用字符串来存储数字,要么使用库来获取任意精度的数字,或者只是让数字更小。
推荐阅读
- ruby-on-rails - 在rails中将字符串转换为整数
- android - 获取根目录内容
- javascript - 如何用参数模拟点击按钮
- c# - 检查用户在 radgridview 的过滤器列中输入了什么
- validation - 如何在 Angular 6 表单控件验证器中一次仅显示一个验证
- c# - 来自组 SpecialServiceRQ 的意外 ERR.SWS.CLIENT.VALIDATION_FAILED
- python - pyusb/libusb0 Windows 权限不足
- eclipse - org.hibernate.TransactionException:事务超时已过期
- python - Python - 在连接不起作用时替换 NA
- python - scipy.misc.toimage 如何改变图像域?