- Private CRM: an AI CRM where Claude does the data entry and you make the calls · part 4 of 12
- Rust
- CRM
Why my CRM stores money in mills, not cents (hello, KWD)
Cents lose the third decimal of the Kuwaiti dinar. Private CRM stores amounts in mills, keeps totals per currency and splits payments so they always add up.
5 min read
Most money advice says "store cents as integers". That breaks the moment a client in Kuwait sends a price like 12.345 KWD. The Kuwaiti dinar has 1,000 fils, so three decimals, and cents throw the last one away.
Private CRM stores money in mills: thousandths of the currency unit. This post covers the switch, rounding for two-decimal currencies, why totals are never added across currencies, and how payment milestones always add up.
Cents to mills
The first version stored cents. When I added a Settings page where the owner picks the company currency, amounts moved from cents to mills in the same commit, so the Kuwaiti dinar and its three-decimal neighbours (BHD, OMR) keep their third decimal.
The whole thing lives in src/money.rs:
//! Amounts are stored as whole mills: thousandths of the currency unit,
//! so 3-decimal currencies such as KWD keep every fils.
pub const PER_UNIT: i64 = 1000;
/// An entered amount in mills, rounded to the decimal places the currency uses.
pub fn mills(amount: &Decimal, decimals: u32) -> i64 {
let units = amount.round(decimals as i64);
(&units * Decimal::from(PER_UNIT)).round(0).to_i64().unwrap_or_default()
}
/// Whether the amount has no more decimal places than the currency uses.
pub fn fits(amount: &Decimal, decimals: u32) -> bool {
&amount.round(decimals as i64) == amount
}
Input comes in as a Decimal, never a float. An amount with more decimals than its currency uses is refused with "Use at most 3 decimal places", instead of being rounded behind the user's back.
The tests that pin it down
The gap checklist (part 12) asked for unit tests on exactly this, and they read like a spec:
#[test]
fn kwd_keeps_every_fils() {
assert_eq!(mills(&amount("12.345"), 3), 12_345);
assert_eq!(units(12_345, 3), "12.345");
assert_eq!(mills(&amount("0.001"), 3), 1);
}
#[test]
fn two_decimal_currencies_round_half_to_even_cents() {
assert_eq!(mills(&amount("12.345"), 2), 12_340);
assert_eq!(mills(&amount("12.355"), 2), 12_360);
}
Two-decimal currencies round half to even: 12.345 USD becomes 12.34 and 12.355 becomes 12.36. That's banker's rounding, and it keeps long columns of rounded numbers from drifting upward.
Taxes as basis points, frozen on the order
Taxes came next. The owner turns tax on, sets a rate, lets products carry their own rate, and picks whether prices include tax or add it on top. Rates are stored as basis points, so 5.25% is 525, an integer again.
The important part: orders keep the rate and amount they were placed with. Change the tax rate next month and old orders don't move. The order page shows the amount before tax and the tax.
Every deal gets its own currency
The single company currency held until the sales team asked for deals in other currencies (part 6). A deal now has its own currency, defaulting to the company's, and AUD joined the list.
That created a trap. The pipeline board added up deal values per column. With USD and KWD deals in one column, the sum was nonsense: 1,200 dollars plus 300 dinars is not 1,500 of anything.
So money is never added across currencies any more. The MCP pipeline_summary tool now returns:
{ "value": { "USD": 1200, "KWD": 300 } }
instead of one number. That changed its shape for any agent reading it, and the pull request said so. On the pipeline page, board columns, headline cards and the list footer show one figure per currency. The dashboard's open pipeline and won cards only count deals in the company currency, because a dashboard tile with three currencies stacked in it helps nobody.
Splitting payments so they always add up
When a won deal becomes a project, 50/30/20 payment terms make three milestones, each with a draft invoice. Percentages and three-decimal currencies don't divide cleanly, so each share is rounded to the currency's smallest coin and the last share takes whatever is left:
/// Splits a price by percent, rounded to the currency's smallest coin; the last share takes the rounding.
pub fn split(price_mills: i64, percents: &[i64], currency: &str) -> Vec<i64> {
let coin = 10_i64.pow(3 - setting::decimals(currency));
let mut shares: Vec<i64> = percents
.iter()
.map(|percent| ((price_mills * percent) as f64 / 100.0 / coin as f64).round() as i64 * coin)
.collect();
let before_last: i64 = shares.iter().rev().skip(1).sum();
if let Some(last) = shares.last_mut() {
*last = price_mills - before_last;
}
shares
}
On a KWD 1000.500 deal the milestones come out at 500.250, 300.150 and 200.100. For USD the coin is 10 mills, one cent. Whatever the rounding does, the invoices add up to the price.
A price that doesn't exist yet
The last money change came with the services catalog (pull request #4). The team wanted to load 15 packages and 36 add-ons before any prices were approved.
The lazy option was "price 0 until we know". That's a lie the system would believe: a 0 price can be ordered and invoiced, and it lands in totals. So price_mills became nullable. An empty price stays empty in the database, the form ("Leave empty until the price is approved") and over MCP, and never turns into 0.
Each catalog item also got its own currency. Orders are totalled in the company currency, so an item with no price, or priced in another currency, can't be ordered and doesn't appear on the order form. That's the only place a product price feeds a total, so it's the only place that needed the guard. Lists and the deal form show "No price yet".
The rules, short
- Store integers in the smallest unit any of your currencies need. For the Gulf, that's mills.
- Parse input as a decimal and refuse extra digits instead of rounding silently.
- Freeze rates on the record that used them.
- Never add two currencies together.
- Split with rounding to the coin, and let the last share absorb the difference.
- Let "unknown" be null, not zero.
If you're building invoicing next, my post on building an invoice generator in one day covers the document side.
Part 5 is the part I get asked about most: the MCP server that lets AI agents work inside the CRM.
Last updated 4 Dec 2026 · filed under Rust, crm