Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.
| Money is written one way. A single formatter turns millionths of a dollar into dollars, rounding half up on whole micros rather than on a float, so the same sum reads the same on every page: under a cent reads <$0.01 on a total and exactly nothing is $0.00, while the statement's lines, a session's receipt, the price book and an agent's effort costs carry up to four places where the fraction of a cent is the point; the six formatters that each rounded their own way are gone. This month is the calendar month in UTC from its first day to today everywhere, and billing counts the month's not-yet-closed usage in any range that reaches into the current month, so the top bar's pill, Spend, Home and Usage ask for the same days and get the same figure; Home now reads the usage report the others read instead of adding up statements. Usage's pending sentence says what of that usage the close will charge after the discount and included usage, which the billing API returns as pending_charged_micros. A reconciliation test holds the pill, Spend, Home and Usage to one number for one month. The usage and billing guide says how amounts are written and what this month means. | 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; | |
| 3 | ||
| 4 | import { microsOf, money, plainDollars, wholeDollars } from "./money.ts"; | |
| 5 | ||
| 6 | test("money rounds half up at the cent on whole micros, never on a float", () => { | |
| 7 | assert.equal(money(2_760_000), "$2.76"); | |
| 8 | assert.equal(money(10_404_999), "$10.40"); | |
| 9 | assert.equal(money(10_405_000), "$10.41"); | |
| 10 | assert.equal(money(10_410_000), "$10.41"); | |
| 11 | assert.equal(money(1_005_000), "$1.01"); | |
| 12 | assert.equal(money(1_004_999), "$1.00"); | |
| 13 | // 1.005 as a float rounds down with toFixed; on micros it is exactly half, so up. | |
| 14 | assert.equal(money(1.005 * 1_000_000), "$1.01"); | |
| 15 | assert.equal(money(999_995_000), "$1,000.00"); | |
| 16 | assert.equal(money(1_234_567_890_000), "$1,234,567.89"); | |
| 17 | }); | |
| 18 | ||
| 19 | test("a fraction of a cent reads <$0.01, and exactly nothing reads $0.00", () => { | |
| 20 | assert.equal(money(0), "$0.00"); | |
| 21 | assert.equal(money(1), "<$0.01"); | |
| 22 | assert.equal(money(3), "<$0.01"); | |
| 23 | assert.equal(money(4_000), "<$0.01"); | |
| 24 | assert.equal(money(9_000), "<$0.01"); | |
| 25 | assert.equal(money(9_999), "<$0.01"); | |
| 26 | assert.equal(money(10_000), "$0.01"); | |
| 27 | assert.equal(money(-9_000), "<−$0.01"); | |
| 28 | }); | |
| 29 | ||
| 30 | test("negatives lead with a minus sign", () => { | |
| 31 | assert.equal(money(-1_250_000), "−$1.25"); | |
| 32 | assert.equal(money(-10_405_000), "−$10.41"); | |
| 33 | assert.equal(money(-1_000_000_000), "−$1,000.00"); | |
| 34 | }); | |
| 35 | ||
| 36 | test("precise shows up to four places, trimmed down to two", () => { | |
| 37 | assert.equal(money(9_000, { precise: true }), "$0.009"); | |
| 38 | assert.equal(money(6_300, { precise: true }), "$0.0063"); | |
| 39 | assert.equal(money(250, { precise: true }), "$0.0003"); | |
| 40 | assert.equal(money(249, { precise: true }), "$0.0002"); | |
| 41 | assert.equal(money(250_000, { precise: true }), "$0.25"); | |
| 42 | assert.equal(money(2_760_000, { precise: true }), "$2.76"); | |
| 43 | assert.equal(money(24_000, { precise: true }), "$0.024"); | |
| 44 | assert.equal(money(1_234_567_890, { precise: true }), "$1,234.5679"); | |
| 45 | assert.equal(money(0, { precise: true }), "$0.00"); | |
| 46 | assert.equal(money(49, { precise: true }), "<$0.0001"); | |
| 47 | assert.equal(money(50, { precise: true }), "$0.0001"); | |
| 48 | assert.equal(money(-6_300, { precise: true }), "−$0.0063"); | |
| 49 | }); | |
| 50 | ||
| 51 | test("compact is an axis label", () => { | |
| 52 | assert.equal(money(0, { compact: true }), "$0"); | |
| 53 | assert.equal(money(2_000_000, { compact: true }), "$2"); | |
| 54 | assert.equal(money(500_000, { compact: true }), "$0.50"); | |
| 55 | assert.equal(money(1_000, { compact: true }), "$0.001"); | |
| 56 | assert.equal(money(1_500_000_000, { compact: true }), "$1.5K"); | |
| 57 | assert.equal(money(1_000_000_000, { compact: true }), "$1K"); | |
| 58 | assert.equal(money(12_340_000_000, { compact: true }), "$12.3K"); | |
| 59 | assert.equal(money(2_500_000_000_000, { compact: true }), "$2.5M"); | |
| 60 | }); | |
| 61 | ||
| 62 | test("whole dollars drop the cents only when there are none", () => { | |
| 63 | assert.equal(wholeDollars(1_000_000_000), "$1,000"); | |
| 64 | assert.equal(wholeDollars(100_000), "$0.10"); | |
| 65 | assert.equal(wholeDollars(-5_000_000), "−$5"); | |
| 66 | }); | |
| 67 | ||
| 68 | test("form fields read plain dollars, rounded the same way", () => { | |
| 69 | assert.equal(plainDollars(12_500_000), "12.50"); | |
| 70 | assert.equal(plainDollars(20_000_000), "20.00"); | |
| 71 | assert.equal(plainDollars(20_000_000, { whole: true }), "20"); | |
| 72 | assert.equal(plainDollars(20_125_000, { whole: true }), "20.13"); | |
| 73 | assert.equal(plainDollars(20_124_999, { whole: true }), "20.12"); | |
| 74 | assert.equal(plainDollars(0, { whole: true }), "0"); | |
| 75 | assert.equal(plainDollars(1_200_000_000), "1200.00"); | |
| 76 | }); | |
| 77 | ||
| 78 | test("dollars from a service that counts in dollars become whole micros", () => { | |
| 79 | assert.equal(microsOf(0.123456789), 123_457); | |
| 80 | assert.equal(microsOf(2.5), 2_500_000); | |
| 81 | assert.equal(money(microsOf(0.004)), "<$0.01"); | |
| 82 | }); |