Representing prices and money in binary: floats, decimals and overflow
Why binary formats rarely send prices as text or floating point, how fixed-point integers and scaled decimals keep them exact, how precision, scale and overflow behave, and when to choose each representation.
On a trading wire, a price like 62900.01 has to survive millions of updates a second, exact tick comparisons, order books keyed by price, and end-of-day accounting down to the last cent or satoshi. How you encode that number in bytes decides both how fast your system runs and whether two sides ever disagree by 0.000000000002.
There is no single best numeric type for everything in finance. Text, binary floating point, fixed-point integers and scaled decimal types each trade exactness, range, size and speed differently. Knowing those trade-offs, and what happens when arithmetic overflows, matters far more than any one format's syntax.
Why prices are not sent as text
You could send "62900.01" as 8 ASCII bytes (36 32 39 30 30 2e 30 31), the way JSON and the older text form of FIX do. Text is readable and exact: it never rounds anything on the wire.
The cost is paid twice on every message:
- Variable width:
"0.5"is 3 bytes while"62900.01000000"is 14, which rules out fixed offsets (how a binary message is put together). - Parsing and formatting: a processor cannot add or compare ASCII digits directly. Every receiver has to turn characters into a binary number on arrival, and every sender has to turn numbers back into characters, work that dominates the time spent on a busy feed.
Text makes sense for APIs that people read or for rare configuration messages, not for high-frequency market data or order entry.
Binary floating point: what float and double are good at, and where they fail
Processors already have fast number types with a fractional part: IEEE 754 binary floating point (float in 4 bytes, double in 8). A double stores a sign bit, a binary exponent and 53 bits of significant digits (about 15 to 17 decimal digits), covers numbers from about 10^−308 to 10^308, and modern processors compute with it in a few cycles, several values at a time.
That makes float and double the right tool for analytics and statistics: implied volatility, option Greeks, moving averages, correlation matrices, risk simulations and machine-learning features. Those calculations need wide range and fast hardware, and their inputs are estimates where 15 digits are more than enough.
They fail for exact prices, quantities and money for one reason: binary floating point builds fractions from powers of two (1/2, 1/4, 1/8, …), whereas money is counted in powers of ten (1/10, 1/100, …). Just as 1/3 never ends in base 10 (0.3333…), 1/10 and 1/100 never end in base 2, so a double holds the nearest value it can:
| Decimal | Value held by a double | Error |
|---|---|---|
| 0.10 | 0.1000000000000000055511151231257827021181583404541015625 | +5.55 × 10^-18 |
| 62900.01 | 62900.0100000000020372681319713592529296875 | +2.04 × 10^-12 |
A 32-bit float keeps only 24 bits of significant digits (about 7 decimal digits), so 62900.01 becomes 62900.01171875, off by about a sixth of a cent before any arithmetic. Even with a 64-bit double, subtracting two prices one cent apart (62900.01 − 62900.00) gives 0.010000000002037268, and 0.1 + 0.2 == 0.3 is false.
That small drift breaks real systems: an order book keyed by a floating-point price splits one price level into two keys; a tick-size check rejects a valid limit order; a ledger of fills drifts a cent away from the exchange's balance after thousands of partial fills.
Precision, scale and exponent
To keep decimal numbers exact in binary, you swap the powers of two for powers of ten. Every exact decimal is an integer mantissa (its digits, without the decimal point) paired with a power of ten:
62900.01is mantissa6290001with exponent−2(or scale2):6290001 × 10^−2 = 6290001 / 100.3.89678000BTC is mantissa389678000with exponent−8(or scale8):389678000 × 10^−8.
Two words come up everywhere and are easy to mix up:
- Precision is the most significant decimal digits a type can hold. A signed 64-bit integer holds any 18-digit number; its largest value is
9,223,372,036,854,775,807. - Scale is how many digits sit right of the decimal point, a count like
2or8. Exponent is the signed power of ten,−2or−8: the same number with the opposite sign.
Watch the sign when moving between systems: multiplying by 10^+2 instead of 10^−2 turns a 62,900.01 USDT Bitcoin quote into 629,000,100.
Fixed-point integers and scaled decimals
Once prices are stored as mantissa × 10^exponent, there is a choice: does every value share one scale, or does each value (or each message) carry its own?
Fixed-point (one fixed scale)
In fixed-point, the format fixes the scale once, say 4 decimal places for equities or 8 for crypto, and only the integer mantissa travels on the wire. Comparing, adding, subtracting and hashing prices are then single integer instructions with no scales to line up: the fastest exact representation a processor can run. The cost is rigidity: if a venue lists both a 100,000 asset ticking in 0.01 and a 0.00000412 token ticking in 10^−8, one shared scale has to fit both, pushing large prices toward overflow or wasting digits on small ones.
Per-value or per-message floating decimal
A floating decimal sends an exponent with the mantissa, or once per message for several prices. The best-bid-and-ask message from how a binary message is put together carries a 1-byte priceExponent once, then 8-byte integer mantissas for bidPrice and askPrice:
| Offset | Bytes | Field | Type | Value | Meaning |
|---|---|---|---|---|---|
| 28 | fe | priceExponent | exponent8 | -2 | × 10^-2 |
| 30–37 | 50 fa 5f 00 00 00 00 00 | bidPrice | mantissa64 | 6290000 | 62900.00 |
| 46–53 | 51 fa 5f 00 00 00 00 00 | askPrice | mantissa64 | 6290001 | 62900.01 |
With both prices at the same exponent, comparing them is one integer comparison (6290001 > 6290000) and the spread is one exact subtraction: 6290001 − 6290000 = 1 tick, 0.01.
One step further is a decimal type with its own scale in every value, in application code: Rust's rust_decimal::Decimal and .NET's decimal pack a 96-bit integer mantissa, a sign and a scale from 0 to 28 into 16 bytes (28 to 29 decimal digits; the largest mantissa is 79,228,162,514,264,337,593,543,950,335). Each number carries its own decimal point, so 62,900.01 (scale 2) and 0.00000001 BTC (scale 8) sit in the same type without any schema change. The cost is twice the memory and arithmetic in software: before adding two values with different scales, one mantissa has to be rescaled to match the other.
Here are the four options side by side:
| Representation | Size | Exact in base 10 | Speed | Best fit |
|---|---|---|---|---|
| ASCII text | varies | yes | slow (parsing) | JSON APIs, logs, configuration |
| float / double | 4 or 8 bytes | no (base 2) | fastest (hardware) | statistics, Greeks, machine learning |
| fixed-point int64 | 8 bytes | yes | fastest (integer) | order books, feeds, matching |
| 128-bit decimal | 16 bytes | yes | moderate (software) | ledgers, accounting across assets |
Overflow: why multiplying two safe numbers breaks
Fixed-width integers are fast because they have hard limits. A signed 64-bit integer (int64) tops out at 9,223,372,036,854,775,807, just over 9.22 × 10^18. At scale 8 that holds prices up to about 92.2 billion: plenty for any single price or quantity.
The trap is multiplying two scaled integers, for example a trade's notional value, price times quantity: the scales add. A scale-8 price times a scale-8 quantity is a scale-16 integer:
| Value | Scale | As an integer | Fits in int64 |
|---|---|---|---|
| price 62,900.01 | 8 | 6,290,001,000,000 | yes |
| quantity 1.50000000 BTC | 8 | 150,000,000 | yes |
| product | 16 | 943,500,150,000,000,000,000 | no: 21 digits |
| int64 maximum | 9,223,372,036,854,775,807 | 19 digits |
Just 1.5 BTC at 62,900.01 overflows int64 about a hundred times over, before dividing by 10^8 to get back to scale 8. With 10 BTC, a 64-bit multiplication that wraps around gives a negative notional, −338,729,134,957,101,056.
Code meets integer overflow in one of four ways:
- Wrapping: the high bits are dropped and the result wraps around modulo
2^64. Rust does this in release builds unless told otherwise (debug builds stop with an error instead); in C and C++ signed overflow is undefined behaviour, which in practice usually wraps. Never acceptable for prices or balances: a large buy silently turns negative. - Saturating: the result sticks at the largest or smallest value instead of wrapping. Fine for a display or a counter, dangerous for accounting because the excess silently disappears.
- Checked: the operation reports overflow as an error (
checked_mul,checked_addin Rust), or the calculation runs in 128-bit integers (i128, up to about1.7 × 10^38) before scaling back to 64 bits. Safe, though a check inside every operation of a hot loop adds branches and error handling. - Validate at the boundary (the trading pattern): check once, when a message enters the system, that prices and quantities lie inside tight limits for the instrument (for instance
0 < price <= 10^12and0 < qty <= 10^9), and compute in a width (i128) whose limit is provably abovemax price × max quantity. With inputs checked at the edge, the matching and pricing code inside cannot overflow, and the hot path needs no checks.
When to choose what
floatordoublefor statistical models, volatility, Greeks, indicators and research, where speed and range matter more than exact cents.- Fixed-point
int64(withi128for intermediate results and checks at the boundary) for market-data feeds, order books, matching and risk engines that need exact ticks at full speed. - A decimal with its own scale (a mantissa and exponent on the wire, or a 128-bit decimal in application code) when one system handles instruments with very different tick sizes, or for accounting, fees and invoices, where exact decimal rounding rules matter more than the last nanosecond.
- Text on a binary wire only when an external API requires it, or for a number too large even for 128 bits.
A note on timestamps: the same integer idea
Binary formats use the same integer idea for timestamps: instead of 27 bytes of ISO 8601 text (2027-03-15T16:00:00.001879Z), a message carries one 8-byte integer counting units of time (milliseconds, microseconds or nanoseconds) since a fixed starting point, the epoch, almost always the Unix epoch, 1970-01-01T00:00:00Z. In the example below, the eventTime field of a Binance best-bid-and-ask update counts microseconds:
| Offset | Bytes | Field | Type | Value | Meaning |
|---|---|---|---|---|---|
| 12–19 | 57 c7 d8 bd c0 69 06 00 | eventTime | utcTimestampUs | 1805126400001879 | 2027-03-15 16:00:00.001879 UTC |
Like a mantissa without its exponent, the integer means nothing without its unit:
| Read as | The instant |
|---|---|
| microseconds, the schema's unit | 2027-03-15 16:00:00.001879 UTC |
| nanoseconds | 1970-01-21 21:25:26.400001879 UTC |
| milliseconds | in the year 59172 |
Reading microseconds as nanoseconds is one of the quickest ways to make every trade look as if it happened in January 1970. Mixing instants in UTC with local wall-clock counts shifts times by the local offset, and again at every daylight saving change.
How common formats handle it
SBE
SBE has no decimal primitive: a schema declares a decimal as a composite pairing an integer mantissa with an exponent, either presence="constant" for fixed-point prices (such as B3 Binary UMDF's constant −4) or an int8 on the wire when tick sizes vary. To save bytes when one message carries several prices, Binance's spot SBE schemas put priceExponent once in the message and point bidPrice and askPrice to it with mbx:exponent. Timestamps are plain 64-bit integer counts since the epoch: nanoseconds by SBE's default (CME MDP 3.0), microseconds on Binance.
Open bidPrice and its mbx:exponent in the Market Data Visualizer
Protobuf
Protobuf has no decimal type. Schemas that care about speed define their own: a fixed-point int64, or a message such as { int64 mantissa; int32 exponent; }. General-purpose APIs often use Google's google.type.Money (int64 units and int32 nanos) or google.type.Decimal (a decimal string, giving up speed for unlimited precision). For time, Protobuf has google.protobuf.Timestamp (int64 seconds and int32 nanos since the Unix epoch), though trading feeds over Protobuf often send a plain int64 or sfixed64 count instead.
Decimals in Parquet
Parquet has a DECIMAL(precision, scale) logical type (physical and logical types): every value in the column shares one non-negative scale and is stored as an integer, in INT32 (up to 9 digits), INT64 (up to 18 digits, like price in the bundled trades sample), or a big-endian FIXED_LEN_BYTE_ARRAY for wider numbers (like the 9-byte notional). A price column stored as DOUBLE instead (like px_float in the trades sample) holds most prices only approximately, for the reasons in binary floating point, so the app's Diagnostics flag it with a warning (Parquet checks). Parquet also marks INT64 TIMESTAMP columns with their unit (MILLIS, MICROS or NANOS) and whether they are instants in UTC or local wall-clock times.
See price and px_float in the trades sample's rows
Sources
- https://docs.python.org/3/tutorial/floatingpoint.html
- https://github.com/FIXTradingCommunity/fix-simple-binary-encoding/blob/master/v1-0-STANDARD/doc/02FieldEncoding.md
- https://github.com/binance/binance-spot-api-docs/blob/master/sbe/schemas/stream_1_0.xml
- https://github.com/binance/binance-spot-api-docs/blob/master/faqs/sbe_faq.md
- https://docs.rs/rust_decimal/latest/rust_decimal/
- https://protobuf.dev/reference/protobuf/google.protobuf/#timestamp
- https://github.com/googleapis/googleapis/blob/master/google/type/decimal.proto
- https://github.com/googleapis/googleapis/blob/master/google/type/money.proto
- https://github.com/apache/parquet-format/blob/master/LogicalTypes.md
- https://cmegroupclientsite.atlassian.net/wiki/spaces/EPICSANDBOX/pages/457638617/MDP+3.0+-+SBE+Technical+Headers
Reviewed by Structure on 2026-11-24. Last changed 2026-11-24.