Abdulkader Safi

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

Share
Kuwaiti dinar amount with three decimal places shown next to USD and AUD totals in a CRM pipeline

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

  1. Store integers in the smallest unit any of your currencies need. For the Gulf, that's mills.
  2. Parse input as a decimal and refuse extra digits instead of rounding silently.
  3. Freeze rates on the record that used them.
  4. Never add two currencies together.
  5. Split with rounding to the coin, and let the last share absorb the difference.
  6. 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

Every entry on this project

12 build notes, in order

Including the ones where nothing worked. You are on part 4.

Read the series →

FAQ

Frequently asked questions

How many decimal places does the Kuwaiti dinar have?

Three. One Kuwaiti dinar (KWD) is 1,000 fils, so 12.345 KWD is a real price. The Bahraini dinar (BHD) and Omani rial (OMR) also use three decimals. Storing these amounts in cents drops the last digit, which is why Private CRM stores mills, thousandths of a unit.

What is the best way to store money in a database?

Store whole integers in the smallest unit you need, plus the currency code. Private CRM stores every amount as mills in a 64-bit integer, so 12.345 KWD is 12345 and 19.99 USD is 19990. Converting to text only happens for display, using the currency's own number of decimals.

Should you add totals across different currencies?

No. Private CRM never adds amounts in different currencies. Pipeline totals come back as one figure per currency, such as {"USD": 1200, "KWD": 300}, and board columns and headline cards show each currency on its own. Converting would need an exchange rate and a date, which a pipeline total doesn't have.

How do you split a payment into percentages without losing a cent?

Round each share to the currency's smallest coin, then give the last share whatever is left over. In Private CRM a 1000.500 KWD deal on 50/30/20 terms splits into 500.250, 300.150 and 200.100, and the milestones always add up to the deal value.

Written by

Abdulkader Safi

Software Engineer

Lead engineer at DSRPT, from Lebanon and based in Kuwait. I write about the tools and bugs from real client work, with the numbers I measured.

About me → GitHub LinkedIn

Need this kind of work done on your project?

Start a project →

Keep reading

More from Private CRM: an AI CRM where Claude does the data entry and you make the calls

All 12 entries →