📜  四舍五入到两个打字稿代码示例的最佳方法

📅  最后修改于: 2022-03-11 14:48:37.206000             🧑  作者: Mango

代码示例1
const roundTo = function(num: number, places: number) {
  const factor = 10 ** places;
  return Math.round(num * factor) / factor;
};

roundTo(123.456, 2);  // 123.46
roundTo(123.3210, 2);  // 123.32
roundTo(123.456, 1);  // 123.5
roundTo(123, 2);  // 123