Optimization of Ethereum prices in Maridb: Prevention of rounding **
Introduction
In this article, we will explore why your Ethereum prices are rounded when entered in the Mariadb database. We will then provide a solution to prevent this rounding and ensure exact data storage.
problem
Let’s take a look at some sample data for demonstrative purposes:
`Json
{
"Timestamp": "2022-02-16T14: 30: 00.000Z",
"Priceusd": 1.23456789
}
Note that the price is rounded at 1.23 when it is entered in the database using Mariadb's "insertion" statement.
problem
This rounding occurs because of the way the prices are kept in a decimal format (for example,float64). When entering data, the database may not accurately represent the initial value due to rounding errors or precision limitations. As a result, the resulting price is rounded to the nearest whole number.
Solution
To prevent rounding and ensuring precise data storage, we can use the following techniques:
1. Distributed as a decimal
We can throw the price column at "Zecimal (8,2) instead of float64
. This will allow a more precise representation of decimal numbers.
`Python
imports the panda as pd
Sample data
data = {
"Timestamp": ["2022-02-16T14: 30: 00.000Z", "2022-02-16T15: 01: 45.678Z"],
"Priceusd": [1.23456789, 1.34567890]
}
Create a datoframe
DF = pd.dataframe (data)
#The pricing column at decimal
DF ["Priceusd"] = DF ["Priceusd"]. Astype ("Decimal")
Enter data in the database
df.to_sql ("my_table", con = motor, if_exist = "append", index = false)
2. Use the decimal library
Alternatively, we can use the Python's "decimal" library to manage decimal arithmetic and prevent rounding.
Python
Decimal import
From decimal decimal decimal, Getcontext
Set precision for decimal operations
Getcontext (). Prec = 20
Sample data
data = {
"Timestamp": ["2022-02-16T14: 30: 00.000Z", "2022-02-16T15: 01: 45.678Z"],
"Priceusd": [decimal ("1.23456789"), decimal ("1.34567890")]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
}
Create a datoframe
DF = pd.dataframe (data)
Convert price columns into decimal
For column in DF.Columns:
If Isinstance (DF [column] .dtype, float):
DF [column] = DF [column]. astype (decimal)
3. Update type of column
If the type of column is already defined asfloat64, we can update it to decimal using the following syntax:
alter table my_table Modify the Priceusd Decimal column (8,2);
Conclusion
By applying one of these techniques, you should be able to prevent rounding and ensure the exact storage of data in the Mariadb database. Remember to adjust the type of column according to your specific requirements.
Example of use cases
- Streaming Ethereum Prices with APIbinance`: You can use this technique to display high frequency price data.
- Analysis of historical prices: The precise representation of prices is crucial for statistical analysis and views.
Following these steps, you will be able to optimize the database scheme to store ETHREUM prices.
Leave a Reply