You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将数字的最后5位置为0?附JavaScript示例代码

如何修改数字的最后5位为指定值

要实现将数字的最后5位替换为指定值,通过简单的数学运算即可完成,核心思路是剥离原数字的前半部分,再与目标末尾组合:

  • 确定除数:使用100000(10的5次方),用于剥离原数字的最后5位
  • 提取前缀部分:将原数字除以除数后取整,得到去掉最后5位的前半部分
  • 组合新数字:前缀部分乘以除数,加上你想要的最后5位数值

代码示例(JavaScript)

针对你给出的例子,将最后5位改为0:

const num = 1666297292886;
const targetLastFive = 0;
const divisor = 100000;

// 提取去掉最后5位的前缀
const prefix = Math.floor(num / divisor);
// 拼接得到结果
const result = prefix * divisor + targetLastFive;

console.log(result); // 输出:1666297200000

如果需要将最后5位改为其他数值(比如12345),只需修改targetLastFive的值:

const num = 1666297292886;
const targetLastFive = 12345;
const divisor = 100000;

const prefix = Math.floor(num / divisor);
const result = prefix * divisor + targetLastFive;

console.log(result); // 输出:1666297212345

注意:targetLastFive需要是0到99999之间的整数,否则会改变原数字的位数长度。

内容的提问来源于stack exchange,提问作者vinograd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 19:56:08