<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="171199" data-post-id="171199">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="LostKobrakai" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/LostKobrakai/120/3072_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  LostKobrakai
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>To be fair: Rounding probably falls in the category of “operate on a float”, but rounding is certainly more common for “view” related logic than arithmetic.</p>
<p>I was recently looking into a php based money handling library using floats and <code>round(float, precision)</code> and was wondering how I could bring the point across that floats are not really a great for money, but couldn’t find a way to break it. Interestingly enough in php round does work like expected with floats: <code>round(round(2.675, 2)) == float(2.68)</code>. Now I’m even more curious.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="171199" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-encode-decimal-with-jason-library-to-float/15107/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-171199" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="171199"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="171200" data-post-id="171200">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="zenw0lf" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zenw0lf/120/3716_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  zenw0lf
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Oh, I see. Thanks for explanation.</p>
<p>So the real solution would be to also use a decimal library on the frontend?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="171200" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-encode-decimal-with-jason-library-to-float/15107/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-171200" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="171200"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="171201" data-post-id="171201">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="benwilson512" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/benwilson512/120/1457_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  benwilson512
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Author of Craft GraphQL APIs in Elixir with Absinthe</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="zenw0lf" data-post="13" data-topic="15107">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zenw0lf/48/3716_2.png" class="avatar"> zenw0lf:</div>
<blockquote>
<p>So the real solution would be to also use a decimal library on the frontend?</p>
</blockquote>
</aside>
<p>Yeah if people care about precision, they should avoid floats at all layers and use decimal libraries at all layers.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="171201" data-batch-url="/posts/batch_likers">
                        3
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-encode-decimal-with-jason-library-to-float/15107/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-171201" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="171201"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="171236" data-post-id="171236">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="kip" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kip/120/1440_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  kip
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>ex_cldr Core Team</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>It depends on whether the rounding happens on the binary data (which is what Elixir does in <a href="https://github.com/elixir-lang/elixir/blob/v1.10.2/lib/elixir/lib/float.ex#L257-L367" rel="nofollow">Float.round/2</a> or on the decimal digits (which I what I do in <a href="https://github.com/elixir-cldr/cldr_utils/blob/master/lib/cldr/utils/math.ex#L773-L969" rel="nofollow">Cldr.Math.round/2</a>).  Perhaps PHP rounds using the decimal digits?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="171236" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-encode-decimal-with-jason-library-to-float/15107/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-171236" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="171236"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #14"></div>
  </section>
</div>
    <div class="postbit" id="171240" data-post-id="171240">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="sribe" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  sribe
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="zenw0lf" data-post="9" data-topic="15107">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zenw0lf/48/3716_2.png" class="avatar"> zenw0lf:</div>
<blockquote>
<p>Is there an example of a decimal string representation that when converted to a float loses precision?</p>
</blockquote>
</aside>
<p>0.1, because there is no finite sum of powers of 2 that equals that</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="171240" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-encode-decimal-with-jason-library-to-float/15107/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-171240" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="171240"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #15"></div>
  </section>
</div>
    <div class="postbit" id="357546" data-post-id="357546">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="gregors" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/gregors/120/17032_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  gregors
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>For legacy code reasons I also had this hard requirement. The following code solves my problem for those with similar requirements.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule CustomTypes.Float do                                                                                                                                                                                                              
                                                                                                                                                                                                                                            
  defstruct value: 0                                                                                                                                                                                                                        
                                                                                                                                                                                                                                            
defimpl Jason.Encoder, for: CustomTypes.Float  do                                                                                                                                                                                           
  def encode(%CustomTypes.Float{value: value}, _opts) do                                                                                                                                                                                    
     Decimal.to_string(value, :normal)                                                                                                                                                                                                      
  end                                                                                                                                                                                                                                       
end                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                            
  def new(%Decimal{} = value) do                                                                                                                                                                                                            
    %__MODULE__{value: value}                                                                                                                                                                                                               
  end                                                                                                                                                                                                                                       
end
</code></pre>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex(1)&gt; CustomTypes.Float.new(Decimal.new("1000.0")) |&gt; Jason.encode
{:ok, "1000.0"}
iex(2)&gt; Decimal.new("1000.0") |&gt; Jason.encode
{:ok, "\"1000.0\""}
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="357546" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-encode-decimal-with-jason-library-to-float/15107/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-357546" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="357546"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #16"></div>
  </section>
</div>
    <div class="postbit" id="357557" data-post-id="357557">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="wojtekmach" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/wojtekmach/120/999_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  wojtekmach
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Hex Core Team</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thanks for writing down what worked for you, always appreciated!</p>
<aside class="quote no-group" data-username="gregors" data-post="17" data-topic="15107">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/gregors/48/17032_2.png" class="avatar"> gregors:</div>
<blockquote>
<pre data-code-wrap="elixir"><code class="lang-elixir">  def encode(%CustomTypes.Float{value: value}, _opts) do                                                                                                                                                                                    
     Decimal.to_string(value, :normal)                                                                                                                                                                                                      
  end 
</code></pre>
</blockquote>
</aside>
<p>My only suggestion would be to add the following check to catch values that’d be considered invalid:</p>
<pre data-code-wrap="diff"><code class="lang-diff">+ if Decimal.inf?(value) or Decimal.nan?(value) do
+   raise "cannot encode #{inspect(value)} to JSON"
+ end

  Decimal.to_string(value, :normal)
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="357557" data-batch-url="/posts/batch_likers">
                        3
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-encode-decimal-with-jason-library-to-float/15107/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-357557" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="357557"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>