// Convert a USD amount to Pico USD (1 USD = 1e12 pico USD) with round-half-up at 12 decimals.
const USD_SCALE = 1_000_000_000_000n; // 1e12
export function usdToPico(usd: string | number): bigint {
const s = typeof usd === "number" ? usd.toString() : usd;
const clean = s.trim();
if (!clean || !/^\d*(?:\.\d*)?$/.test(clean)) {
throw new Error("Invalid USD amount");
}
const [intPart = "0", frac = ""] = clean.split(".");
const frac12 = (frac + "000000000000").slice(0, 12); // pad/truncate to 12
const nextDigit = frac.length > 12 ? Number(frac[12]) : 0; // 13th digit for rounding
let scaled = BigInt(intPart || "0") * USD_SCALE + BigInt(frac12 || "0");
if (nextDigit >= 5) scaled += 1n; // round-half-up
return scaled;
}
// Examples
const p1 = usdToPico("0.0035"); // 3500000000n
const p2 = usdToPico(0.1); // 100000000000n
// If your API expects a string or JSON value instead of BigInt:
const pricePicoUSD = usdToPico("0.0035").toString();