4Manuals

  • PDF Cloud HOME

如何从模中得到商 Download

    CSS / JS-如何覆盖一个孩子而不是其他孩子的父母过渡 反应:更改方向后触发重新渲染和offsetWidth的重新计算 我的Javascript从'string'转换为'integer'或'number'无效 如何删除外部索引并合并内部json 在React Native 图片无法通过javascript刷新 如何在react图表上的datakey中操作DateTime 在Cypress中保存变量 如何从作为请求传递的输入类型数据绑定中提取数据 通过dropzone laravel通过电子邮件发送多个图像

是否有一种简单的方法可以将2中的商2从10%中减去?我是否必须手动重新计算   21-21%10

1 个答案:

答案 0 :(得分:0)

使用/代替%,然后向下舍入:

console.log(
  Math.floor(21 / 10)
);

如果代码速度真的是一个问题(几乎总是没有),尤其是对于以10为底的数字,除以然后再使用Math.floor看起来比将数字转化为整数要快得多。一个字符串,并获取除前一位以外的所有数字:

const length = 10_000_000;
const bigArr = Array.from(
  { length },
  () => Math.ceil(Math.random() * 100)
);
const smallArr = Array.from(
  { length },
  () => 10
);

setTimeout(() => {
  const t0 = performance.now();
  for (let i = 0; i < length; i++) {
    const quotient = Math.floor(bigArr[i] / smallArr[i]);
  }
  const t1 = performance.now();
  console.log(t1 - t0);
}, 1000);

const length = 10_000_000;
const bigArr = Array.from(
  { length },
  () => Math.ceil(Math.random() * 100)
);
const smallArr = Array.from(
  { length },
  () => 10
);

setTimeout(() => {
  const t0 = performance.now();
  for (let i = 0; i < length; i++) {
    const str = String(bigArr[i])
    const quotient = str.slice(0, str.length - 1);
  }
  const t1 = performance.now();
  console.log(t1 - t0);
}, 1000);



Similar searches
    无法访问https://storage.googleapis.com/ 使用Bitbucket在Jenkins中使用本地Maven存储库的问题 发送字符串作为参数时出现段错误 C#.net核心控制台应用程序Debug.Assert不显示消息框 如何将来自不同应用程序的数据拆分为自己的索引?