如何在javascript中比较两个大数

问题描述 投票:0回答:1

为什么下面的代码输出结果是相等的?如何比较?

var num1 = 340282000000000000000000000000000000001
var num2 = 340282000000000000000000000000000000000  // 3.402823E+38
if(num1 == num2) {
    console.log('num1 equal to num2');
} else if(num1 > num2) {
    console.log('num1 bigger than num2');
} else {
    console.log('num1 lower than num2')
}
javascript numbers
1个回答
0
投票

尝试将它们定义为

BigInt

var num1 = 340282000000000000000000000000000000001n;
var num2 = 340282000000000000000000000000000000000n;
if(num1 == num2) {
    console.log('num1 equal to num2');
} else if(num1 > num2) {
    console.log('num1 bigger than num2');
} else {
    console.log('num1 lower than num2')
}

© www.soinside.com 2019 - 2024. All rights reserved.