小程序
小游戏
企业微信
微信支付
扫描小程序码分享
用if判断两个整数的大小关系,如if(a < b){.....},数值比较小的时候判断是正确的,当数值比较大比如两个都是16位的整数时,if判断就经常不准确了,是不是精度丢失的问题?有没有解决的办法?一直没想到比较灵活的办法
3 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
请参考使用 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
16位?big data肯定是有问题的,超过JS支持上限了
给你一个案例
// 两个数做差处理,数字位数超过16位后
export function sumNumber (min, max) {
let pot_l
if (min.length === 1 && max.length === 1) {
return Number(max) - Number(min)
}
function plusOne (i, temp) {
temp[i + 1] = 0
if ((temp[i + 1] + 1 < 10)) {
temp[i + 1] = temp[i + 1] + 1
} else {
temp[i + 1] = temp[i + 1] + 1 - 10
plusOne(i + 1, temp)
function decreaseOne (i, arr) {
if (Number(arr[i]) !== 0) {
arr[i] = Number(arr[i]) - 1
arr[i] = 9
decreaseOne(i + 1, arr)
console.log(arr[i])
if (min.indexOf('.') > 0 || max.indexOf('.') > 0) {
const arr1 = min.split('.')
const arr2 = max.split('.')
if (arr1.length > 1 && arr2.length === 1) {
min = [...arr1].join('')
max = [...arr2, '0'.repeat(arr1[1].length)].join('')
pot_l = arr1[1].length
} else if (arr2.length > 1 && arr1.length === 1) {
max = [...arr2].join('')
min = [...arr1, '0'.repeat(arr2[1].length)].join('')
pot_l = arr2[1].length
console.log('fdf', arr1, arr2)
const arr1_l_1 = arr1[1].length
const arr2_l_2 = arr2[1].length
arr1_l_1 > arr2_l_2 && (arr2[1] = arr2[1] + '0'.repeat(arr1_l_1 - arr2_l_2))
arr2_l_2 > arr1_l_1 && (arr1[1] = arr1[1] + '0'.repeat(arr2_l_2 - arr1_l_1))
pot_l = arr1_l_1
min = arr1.join('')
max = arr2.join('')
console.log('min max', min, max)
let arr_1 = Array.from(min).reverse()
let arr_2 = Array.from(max).reverse()
let temp = []
let str = ''
const arr1_l = arr_1.length
const arr2_l = arr_2.length
if (arr_1[arr1_l - 1] === '-' && arr_2[arr2_l - 1] !== '-' || arr_1[arr1_l - 1] !== '-' && arr_2[arr2_l - 1] === '-') {
if (arr_1[arr1_l - 1] === '-' && arr_2[arr2_l - 1] !== '-') {
arr_1.splice(arr1_l - 1, 1)
arr_2.splice(arr2_l - 1, 1)
const tem = arr_1
arr_1 = arr_2
arr_2 = tem
str = '-'
if (arr_1.length >= arr_2.length) {
console.log('进来了1')
console.log('fafa', arr_1, arr_2)
arr_1.forEach((item, i) => {
const arr1_n = Number(arr_1[i])
const arr2_n = Number(arr_2[i])
if (arr2_n && (arr1_n + arr2_n < 10)) {
temp[i] = temp[i] ? temp[i] + arr1_n + arr2_n : arr1_n + arr2_n
} else if (arr2_n && (arr1_n + arr2_n >= 10)) {
temp[i] = temp[i] ? temp[i] + arr1_n + arr2_n - 10 : arr1_n + arr2_n - 10
plusOne(i, temp)
if (temp[i] !== undefined) {
if (Number(temp[i]) + arr1_n > 10) {
temp[i] = Number(temp[i]) + arr1_n - 10
temp[i] = Number(temp[i]) + arr1_n
temp[i] = temp[i] ? temp[i] + arr1_n : arr1_n
})
console.log('进来了2')
arr_2.forEach((item, i) => {
if (arr1_n && (arr2_n + arr1_n < 10)) {
temp[i] = temp[i] !== undefined ? temp[i] + arr2_n + arr1_n : arr2_n + arr1_n
} else if (arr1_n && (arr2_n + arr1_n >= 10)) {
temp[i] = temp[i] !== undefined ? temp[i] + arr2_n + arr1_n - 10 : arr2_n + arr1_n - 10
if (Number(temp[i]) + arr2_n >= 10) {
temp[i] = Number(temp[i]) + arr2_n - 10
temp[i] = Number(temp[i]) + arr2_n
temp[i] = temp[i] ? temp[i] + arr2_n : arr2_n
if (arr_1[arr1_l - 1] === '-' && arr_2[arr2_l - 1] === '-') {
// 最小值得长度大于最大值的长度
if (arr_1.length > arr_2.length) {
if (!(Number.isNaN(arr1_n)) && (arr1_n <= arr2_n)) {
temp[i] = arr2_n - arr1_n
} else if (!(Number.isNaN(arr1_n)) && (arr1_n > arr2_n)) {
if (Number(arr_2[i + 1]) !== 0) {
arr_2[i + 1] = Number(arr_2[i + 1]) - 1
temp[i] = arr2_n + 10 - arr1_n
decreaseOne(i + 1, arr_2)
temp[i] = arr2_n
// 最小值得长度小于等于最大值的长度
// 判断两个数的长度相等,如果最小值大于最大值,交换两个的值
if (arr1_l === arr2_l && arr_1[arr1_l - 1] > arr_2[arr2_l - 1]) {
if ((arr1_n !== undefined) && (arr1_n <= arr2_n)) {
} else if ((arr1_n !== undefined) && (arr1_n > arr2_n)) {
temp[i] = temp[i] ? temp[i] + arr_2[i] : arr_2[i]
pot_l !== undefined && (temp.splice(pot_l, 0, '.'))
temp = str + temp.reverse().join('').replace(/^[0]*/, function (a, b) {
if (pot_l) {
return '0'
return ''
console.log(temp)
return temp
export function compareNumber (min, max) {
console.log('min,max', min, max)
if (!min || !max) return false
const min_l = min.length
const max_l = max.length
const arr_min = min.split('')
const arr_max = max.split('')
let i = 0
console.log('fdf', min, max)
let flag = true
function iterator (a, b) {
console.log('执行了')
console.log(i)
if (arr_min[i] < arr_max[i]) {
console.log('走这里了')
flag = true
} else if (arr_min[i] === arr_max[i]) {
if (i === min_l - 1) {
console.log(i, arr_min[i], arr_max[i])
i += 1
iterator()
flag = false
if (max_l > min_l) {
return true
} else if (max_l === min_l) {
return flag
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
请参考使用 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt
https://github.com/GoogleChromeLabs/jsbi
16位?big data肯定是有问题的,超过JS支持上限了
给你一个案例
// 两个数做差处理,数字位数超过16位后
export function sumNumber (min, max) {
let pot_l
if (min.length === 1 && max.length === 1) {
return Number(max) - Number(min)
}
function plusOne (i, temp) {
temp[i + 1] = 0
if ((temp[i + 1] + 1 < 10)) {
temp[i + 1] = temp[i + 1] + 1
} else {
temp[i + 1] = temp[i + 1] + 1 - 10
plusOne(i + 1, temp)
}
}
function decreaseOne (i, arr) {
if (Number(arr[i]) !== 0) {
arr[i] = Number(arr[i]) - 1
} else {
arr[i] = 9
decreaseOne(i + 1, arr)
}
console.log(arr[i])
}
if (min.indexOf('.') > 0 || max.indexOf('.') > 0) {
const arr1 = min.split('.')
const arr2 = max.split('.')
if (arr1.length > 1 && arr2.length === 1) {
min = [...arr1].join('')
max = [...arr2, '0'.repeat(arr1[1].length)].join('')
pot_l = arr1[1].length
} else if (arr2.length > 1 && arr1.length === 1) {
max = [...arr2].join('')
min = [...arr1, '0'.repeat(arr2[1].length)].join('')
pot_l = arr2[1].length
} else {
console.log('fdf', arr1, arr2)
const arr1_l_1 = arr1[1].length
const arr2_l_2 = arr2[1].length
arr1_l_1 > arr2_l_2 && (arr2[1] = arr2[1] + '0'.repeat(arr1_l_1 - arr2_l_2))
arr2_l_2 > arr1_l_1 && (arr1[1] = arr1[1] + '0'.repeat(arr2_l_2 - arr1_l_1))
pot_l = arr1_l_1
min = arr1.join('')
max = arr2.join('')
}
}
console.log('min max', min, max)
let arr_1 = Array.from(min).reverse()
let arr_2 = Array.from(max).reverse()
let temp = []
let str = ''
const arr1_l = arr_1.length
const arr2_l = arr_2.length
if (arr_1[arr1_l - 1] === '-' && arr_2[arr2_l - 1] !== '-' || arr_1[arr1_l - 1] !== '-' && arr_2[arr2_l - 1] === '-') {
if (arr_1[arr1_l - 1] === '-' && arr_2[arr2_l - 1] !== '-') {
arr_1.splice(arr1_l - 1, 1)
} else {
arr_2.splice(arr2_l - 1, 1)
const tem = arr_1
arr_1 = arr_2
arr_2 = tem
str = '-'
}
if (arr_1.length >= arr_2.length) {
console.log('进来了1')
console.log('fafa', arr_1, arr_2)
arr_1.forEach((item, i) => {
const arr1_n = Number(arr_1[i])
const arr2_n = Number(arr_2[i])
if (arr2_n && (arr1_n + arr2_n < 10)) {
temp[i] = temp[i] ? temp[i] + arr1_n + arr2_n : arr1_n + arr2_n
} else if (arr2_n && (arr1_n + arr2_n >= 10)) {
temp[i] = temp[i] ? temp[i] + arr1_n + arr2_n - 10 : arr1_n + arr2_n - 10
plusOne(i, temp)
} else {
if (temp[i] !== undefined) {
if (Number(temp[i]) + arr1_n > 10) {
temp[i] = Number(temp[i]) + arr1_n - 10
plusOne(i, temp)
} else {
temp[i] = Number(temp[i]) + arr1_n
}
} else {
temp[i] = temp[i] ? temp[i] + arr1_n : arr1_n
}
}
})
} else {
console.log('进来了2')
arr_2.forEach((item, i) => {
const arr1_n = Number(arr_1[i])
const arr2_n = Number(arr_2[i])
if (arr1_n && (arr2_n + arr1_n < 10)) {
temp[i] = temp[i] !== undefined ? temp[i] + arr2_n + arr1_n : arr2_n + arr1_n
} else if (arr1_n && (arr2_n + arr1_n >= 10)) {
temp[i] = temp[i] !== undefined ? temp[i] + arr2_n + arr1_n - 10 : arr2_n + arr1_n - 10
plusOne(i, temp)
} else {
if (temp[i] !== undefined) {
if (Number(temp[i]) + arr2_n >= 10) {
temp[i] = Number(temp[i]) + arr2_n - 10
plusOne(i, temp)
} else {
temp[i] = Number(temp[i]) + arr2_n
}
} else {
temp[i] = temp[i] ? temp[i] + arr2_n : arr2_n
}
}
})
}
} else {
if (arr_1[arr1_l - 1] === '-' && arr_2[arr2_l - 1] === '-') {
arr_1.splice(arr1_l - 1, 1)
arr_2.splice(arr2_l - 1, 1)
str = '-'
}
// 最小值得长度大于最大值的长度
if (arr_1.length > arr_2.length) {
const tem = arr_1
arr_1 = arr_2
arr_2 = tem
str = '-'
arr_2.forEach((item, i) => {
const arr1_n = Number(arr_1[i])
const arr2_n = Number(arr_2[i])
if (!(Number.isNaN(arr1_n)) && (arr1_n <= arr2_n)) {
temp[i] = arr2_n - arr1_n
} else if (!(Number.isNaN(arr1_n)) && (arr1_n > arr2_n)) {
if (Number(arr_2[i + 1]) !== 0) {
arr_2[i + 1] = Number(arr_2[i + 1]) - 1
temp[i] = arr2_n + 10 - arr1_n
} else {
decreaseOne(i + 1, arr_2)
temp[i] = arr2_n + 10 - arr1_n
}
} else {
temp[i] = arr2_n
}
})
// 最小值得长度小于等于最大值的长度
} else {
// 判断两个数的长度相等,如果最小值大于最大值,交换两个的值
if (arr1_l === arr2_l && arr_1[arr1_l - 1] > arr_2[arr2_l - 1]) {
const tem = arr_1
arr_1 = arr_2
arr_2 = tem
str = '-'
}
arr_2.forEach((item, i) => {
const arr1_n = Number(arr_1[i])
const arr2_n = Number(arr_2[i])
if ((arr1_n !== undefined) && (arr1_n <= arr2_n)) {
temp[i] = arr2_n - arr1_n
} else if ((arr1_n !== undefined) && (arr1_n > arr2_n)) {
if (Number(arr_2[i + 1]) !== 0) {
arr_2[i + 1] = Number(arr_2[i + 1]) - 1
temp[i] = arr2_n + 10 - arr1_n
} else {
decreaseOne(i + 1, arr_2)
temp[i] = arr2_n + 10 - arr1_n
}
} else {
temp[i] = temp[i] ? temp[i] + arr_2[i] : arr_2[i]
}
})
}
}
pot_l !== undefined && (temp.splice(pot_l, 0, '.'))
temp = str + temp.reverse().join('').replace(/^[0]*/, function (a, b) {
if (pot_l) {
return '0'
} else {
return ''
}
})
console.log(temp)
return temp
}
export function compareNumber (min, max) {
console.log('min,max', min, max)
if (!min || !max) return false
if (min.indexOf('.') > 0 || max.indexOf('.') > 0) {
const arr1 = min.split('.')
const arr2 = max.split('.')
if (arr1.length > 1 && arr2.length === 1) {
min = [...arr1].join('')
max = [...arr2, '0'.repeat(arr1[1].length)].join('')
} else if (arr2.length > 1 && arr1.length === 1) {
max = [...arr2].join('')
min = [...arr1, '0'.repeat(arr2[1].length)].join('')
} else {
console.log('fdf', arr1, arr2)
const arr1_l_1 = arr1[1].length
const arr2_l_2 = arr2[1].length
arr1_l_1 > arr2_l_2 && (arr2[1] = arr2[1] + '0'.repeat(arr1_l_1 - arr2_l_2))
arr2_l_2 > arr1_l_1 && (arr1[1] = arr1[1] + '0'.repeat(arr2_l_2 - arr1_l_1))
min = arr1.join('')
max = arr2.join('')
}
}
const min_l = min.length
const max_l = max.length
const arr_min = min.split('')
const arr_max = max.split('')
let i = 0
console.log('fdf', min, max)
let flag = true
function iterator (a, b) {
console.log('执行了')
console.log(i)
if (arr_min[i] < arr_max[i]) {
console.log('走这里了')
flag = true
} else if (arr_min[i] === arr_max[i]) {
if (i === min_l - 1) {
flag = true
} else {
console.log(i, arr_min[i], arr_max[i])
i += 1
iterator()
}
} else {
flag = false
}
}
if (max_l > min_l) {
return true
} else if (max_l === min_l) {
iterator()
} else {
flag = false
}
return flag
}