总是出现这个错误:num.toExponential 不是一个函数。 num.toExponential 未定义。
为什么 toExponential()
不能在 React Native 上运行?如何在 React Native 中复制此功能?
function ifLessOneSatoshi(raw_num){
const num = raw_num;
if(raw_num < 0){
num = Math.abs(raw_num);
}
if(num < 0.000001){
if(raw_num < 0){
const abn = abs(num);
return -1 * abn.toExponential();
}
return num.toExponential();
}
return raw_num;
}
以下是我根据云海的回答实现的:
function ifLessOneMillionth(raw_num){
var num = Number.parseFloat(raw_num); //<=this line you may use parseFloat(raw_num)
if(raw_num < 0){
num = Math.abs(raw_num);
}
if(num < 0.000001){
if(raw_num < 0){
//const abn = Math.abs(num);
return -num.toExponential(); // this line
}
return num.toExponential();
}
return raw_num;
}
最佳答案
你遇到的问题更少了。
首先,您重新分配const num
,const
意味着您无法在声明后重新分配变量。所以你应该将其更改为 var
。
其次,如果raw_num
不是“数字对象”,则未定义的原因来自于它。根据格式,您可能需要类似 parseFloat(raw_num)
的内容(我不确定 raw_num 是否是十进制或其他形式)。你需要小心这一点。
那么,最后一个问题是-1 * num.toExponential()
,num.toExponential()返回一个字符串
,所以为了使其为负数,你应该使用字符串连接
而不是数学运算。
function ifLessOneSatoshi(raw_num){
var num = raw_num; //<=this line you may use parseFloat(raw_num)
if(raw_num < 0){
num = Math.abs(raw_num);
}
if(num < 0.000001){
if(raw_num < 0){
const abn = Math.abs(num);
return -abn.toExponential(); // this line
}
return num.toExponential();
}
return raw_num;
}
当我检查你的代码时,有一些奇怪的行为,你必须自己检查这些条件if(raw_num)
和if(num)
而不知道 Intent 逻辑。
关于javascript - toExpontial 在 React Native 中未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60458697/