Capítulo 168 de 411
A small helper that formats a raw number into a comma-grouped string with a fixed number of decimal places (e.g. 1000.254145 → "1,000.25"), handy for animating counters.
toLocaleString("en-US") for comma grouping, then manually pads/truncates the decimal portion to the requested length.onUpdate to render a live-updating formatted counter.function formatNumber(value, decimals) {
let s = (+value).toLocaleString("en-US").split(".");
return decimals ? s[0] + "." + ((s[1] || "") + "00000000").substr(0, decimals) : s[0];
}
let obj = { num: 100 };
gsap.to(obj, {
num: 10500,
onUpdate: () => (myElement.innerText = "$" + formatNumber(obj.num, 2)),
});
onUpdate.