<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Christian Lauridsen</title>
    <description>The latest articles on DEV Community by Christian Lauridsen (@webwizard).</description>
    <link>https://dev.to/webwizard</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4204290%2F0f6f4414-8d0b-4b20-9b3e-cded57d748be.jpg</url>
      <title>DEV Community: Christian Lauridsen</title>
      <link>https://dev.to/webwizard</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/webwizard"/>
    <language>en</language>
    <item>
      <title>Why a crypto price API should send decimal strings, not numbers</title>
      <dc:creator>Christian Lauridsen</dc:creator>
      <pubDate>Sat, 24 Oct 2026 09:26:11 +0000</pubDate>
      <link>https://dev.to/webwizard/why-a-crypto-price-api-should-send-decimal-strings-not-numbers-6df</link>
      <guid>https://dev.to/webwizard/why-a-crypto-price-api-should-send-decimal-strings-not-numbers-6df</guid>
      <description>&lt;p&gt;If you consume a market-data API, you will add, multiply and round prices. A common cause of wrong totals in that code is that the price was already a binary float before the developer's own code ran.&lt;/p&gt;

&lt;p&gt;This post shows where that happens, with examples I ran, and the fix: an API that sends prices as decimal strings, and a client that parses them on purpose.&lt;/p&gt;

&lt;h2&gt;
  
  
  The one-line version
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mf"&gt;0.1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.2&lt;/span&gt;
&lt;span class="mf"&gt;0.30000000000000004&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Binary floating point cannot store most decimal fractions exactly. For a game that is invisible. For money it is the reason accounting software uses decimal types.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three places it bites in a price feed
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Arithmetic on a price
&lt;/h3&gt;

&lt;p&gt;Take a plausible Bitcoin price and multiply it by 3, as a floating-point library would, and as a decimal library would:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;decimal&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Decimal&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;63520.780763913&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="mf"&gt;190562.34229173898&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;63520.780763913&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="nc"&gt;Decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;190562.342291739&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The float answer carries digits the input never had. Nothing is wrong enough to crash, which is the problem: the error only appears when you compare totals, reconcile against another system, or store the result and read it back.&lt;/p&gt;

&lt;p&gt;Accumulation makes it worse:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;     &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mf"&gt;0.1&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt;
&lt;span class="mf"&gt;0.9999999999999999&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mf"&gt;1.0&lt;/span&gt;
&lt;span class="bp"&gt;False&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Summing a thousand ticks or fills is the same loop with more room for the error to grow.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Integers above 2^53
&lt;/h3&gt;

&lt;p&gt;JavaScript numbers are IEEE 754 doubles. Every integer up to &lt;code&gt;Number.MAX_SAFE_INTEGER&lt;/code&gt;, 9007199254740991, is exact; past that, some integers cannot be represented and get rounded to a neighbour. Token amounts in the smallest unit (wei, for example) go far beyond that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;MAX_SAFE_INTEGER&lt;/span&gt;
&lt;span class="mi"&gt;9007199254740991&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1234567890123456789&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="mi"&gt;1234567890123456800&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The last digits are gone, silently. &lt;code&gt;JSON.parse&lt;/code&gt; does the same thing to any number in a response, because JSON has a single &lt;code&gt;number&lt;/code&gt; type and the parser chooses a double:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;{"supply":1234567890123456789}&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;supply&lt;/span&gt;
&lt;span class="mi"&gt;1234567890123456800&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python's &lt;code&gt;json&lt;/code&gt; keeps large integers exact, but it still turns &lt;code&gt;0.1&lt;/code&gt; into a float unless you tell it otherwise.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Rounding at display time
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;2.675&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nc"&gt;Decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;2.67499999999999982236431605997495353221893310546875&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;2.675&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="mf"&gt;2.67&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;2.675&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;quantize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;0.01&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nc"&gt;Decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;2.68&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The float &lt;code&gt;2.675&lt;/code&gt; is really 2.67499999999999982..., so it rounds down. The decimal &lt;code&gt;2.675&lt;/code&gt; is exactly what you wrote, so the rounding rule applies to your number. Note that &lt;code&gt;Decimal&lt;/code&gt; rounds half to even by default (&lt;code&gt;Decimal("0.125")&lt;/code&gt; becomes &lt;code&gt;0.12&lt;/code&gt;), so choose the rule you want on purpose; see the checklist.&lt;/p&gt;

&lt;p&gt;A fair caveat: a single ordinary price such as &lt;code&gt;63520.780763913&lt;/code&gt; survives a trip through a float. The damage shows up in arithmetic, large integers and accumulated sums, as above. That is why it goes unnoticed in testing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why the API is the right place to fix it
&lt;/h2&gt;

&lt;p&gt;An API that sends this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"price"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;63520.780763913&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;has already made the choice for you. By default, your JSON parser turns the number into a double, and your code never sees the original digits. (Some parsers can be told otherwise, as the Python example below shows, but you have to know to ask.)&lt;/p&gt;

&lt;p&gt;An API that sends this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nl"&gt;"price"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"63520.780763913"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;hands the decision to the developer. The string cannot lose precision in transit. It costs one extra step on the client, parsing it into a decimal type, and that step is the point: you now do it deliberately.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://bitculator.com/en/crypto-api" rel="noopener noreferrer"&gt;Bitculator Data API&lt;/a&gt;, which I work on, sends prices, rates and supplies as decimal strings for this reason. I will use it only as the example below.&lt;/p&gt;

&lt;h2&gt;
  
  
  The client side, in four languages
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Python.&lt;/strong&gt; Parse with &lt;code&gt;Decimal&lt;/code&gt;. If you must read numbers from a response that sends floats, &lt;code&gt;json.loads&lt;/code&gt; can do it for you:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;decimal&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Decimal&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;p&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;: 0.1}&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;parse_float&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;Decimal&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;p&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nc"&gt;Decimal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;0.1&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With a string field, &lt;code&gt;Decimal(response["price"])&lt;/code&gt; is enough. The official Python SDK returns prices as strings and its README says to use &lt;code&gt;Decimal&lt;/code&gt; for any arithmetic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JavaScript / TypeScript.&lt;/strong&gt; There is no built-in decimal type. Keep the value as a string until you need math, then use a decimal library, or &lt;code&gt;BigInt&lt;/code&gt; when the quantity is a whole number of smallest units. Never &lt;code&gt;Number(price)&lt;/code&gt; for something you will sum.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Go.&lt;/strong&gt; Decode into a &lt;code&gt;string&lt;/code&gt; field, not a &lt;code&gt;float64&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="c"&gt;// raw is the json.RawMessage the SDK returns&lt;/span&gt;
&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;coin&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Symbol&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="s"&gt;`json:"symbol"`&lt;/span&gt;
    &lt;span class="n"&gt;Price&lt;/span&gt;  &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="s"&gt;`json:"price"`&lt;/span&gt; &lt;span class="c"&gt;// decimal string, never rounded&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Unmarshal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;coin&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then calculate with &lt;code&gt;big.Rat&lt;/code&gt;, which parses and holds a decimal string exactly (&lt;code&gt;SetString("63520.780763913")&lt;/code&gt;), or with a decimal package. Avoid &lt;code&gt;big.Float&lt;/code&gt; for this: it is still binary floating point, just with more bits.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Java.&lt;/strong&gt; &lt;code&gt;new BigDecimal(priceString)&lt;/code&gt;, and beware &lt;code&gt;new BigDecimal(someDouble)&lt;/code&gt;, which captures the float's error too.&lt;/p&gt;

&lt;h2&gt;
  
  
  A short checklist for API consumers
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Check the API's docs: is a price a number or a string? If it is a number, your parser has already chosen a double.

&lt;ol&gt;
&lt;li&gt;Keep prices as strings or decimal types from the wire to the database. Convert to float only for charts, where a pixel is the resolution.&lt;/li&gt;
&lt;li&gt;Never sum floats for anything that must reconcile. Sum decimals, or integers in the smallest unit.&lt;/li&gt;
&lt;li&gt;Decide your rounding rule explicitly (half up, half even) and apply it with a decimal type, once, at the edge.&lt;/li&gt;
&lt;li&gt;Put one test in your suite with a value that breaks floats, such as 0.1 + 0.2 or an integer above 2^53.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Docs and source code
&lt;/h2&gt;

&lt;p&gt;The API reference is at &lt;a href="https://bitculator.com/en/documentation/api/v1" rel="noopener noreferrer"&gt;https://bitculator.com/en/documentation/api/v1&lt;/a&gt;. The SDKs I mentioned are MIT-licensed and have no dependencies. The Python and Go ones are at &lt;a href="https://github.com/Bitculator/bitculator-python-sdk" rel="noopener noreferrer"&gt;https://github.com/Bitculator/bitculator-python-sdk&lt;/a&gt; and &lt;a href="https://github.com/Bitculator/bitculator-go-sdk" rel="noopener noreferrer"&gt;https://github.com/Bitculator/bitculator-go-sdk&lt;/a&gt;. You do not need an API key to read the code.&lt;/p&gt;




</description>
      <category>api</category>
      <category>python</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
