Dev Helpers
← Back

Calculate extended prices without floats

Calculate extended prices without floats. Build the result in an App Router server page and send the rendered entries to the client.

Open this example in the interactive library

Source code

// app/calculate-extended-prices-without-floats/page.tsx
export const metadata = { title: "Calculate extended prices without floats" };
export default function Page() {
  const rows = [
    { item: "tea", units: 2, cents: 350 },
    { item: "mug", units: 1, cents: 900 },
    { item: "tea", units: 3, cents: 325 },
    { item: "tin", units: 2, cents: 500 },
  ];
  const result = rows.map((r) => ({
    item: r.item,
    totalCents: r.units * r.cents,
  }));
  const entries = Object.entries(
    typeof result === "object" && result !== null ? result : { value: result },
  );
  return (
    <section>
      <h2>{"Calculate extended prices without floats"}</h2>
      {entries.length ? (
        <dl>
          {entries.map(([key, value]) => (
            <div key={key}>
              <dt>{key}</dt>
              <dd>
                {typeof value === "string" ? value : JSON.stringify(value)}
              </dd>
            </div>
          ))}
        </dl>
      ) : (
        <p>No matching entries.</p>
      )}
    </section>
  );
}