"12345678901234567890"のように桁数が多い数値の場合、
Number("12345678901234567890") === 12345678901234567000 // true
となってしまいます。
自分の作っているアプリでこのような桁の大きい文字型の数値から1だけマイナスしたかったので下記のような関数を作りました。
export const decrease = (snowflake: string): string | null => {
if (!/^([1-9]\d*|0)$/.test(snowflake)) return null;
let i = 1;
while (snowflake[snowflake.length - i] === "0") { i++; }
const tail = snowflake.slice(-i);
let tailDec = String(Number(tail) - 1);
if (tailDec.length < tail.length) tailDec = "0" + tailDec;
return snowflake.slice(0, -i) + tailDec;
}
実行
console.log(decrease("12345678901234567890"));
// 12345678901234567889