Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
User-Defined Precision for Decimal Places
In the original code, the average was calculated and stored as an integer, meaning any decimal part was lost due to Solidity’s lack of native floating-point support. I added a decimals parameter to the function so the user can specify how many decimal places they want in the result. This gives more flexibility, as the user can define the precision they need.
2. Simulating Decimals in Solidity Since Solidity doesn’t support floating-point numbers, I simulated decimals by multiplying the result by 10^decimals. This approach allows storing the average as an integer while retaining precision. For example, if the average is 456 and the user wants four decimal places, the contract now stores 4560000 (which represents 456.0000). This prevents loss of precision that would occur in calculations where the average isn’t a whole number.
3. Formatting Output as a String I added a helper function, getFormattedAvg, to convert the integer result into a string with the specified number of decimal places. This formatting is more user-friendly and visually represents the average with decimals. The function breaks down the avg into its integer and fractional parts based on the user-defined precision, then formats it as "integer.fractional".
4. Improved Precision Control By letting the user define decimals, this contract can avoid excessive precision that isn’t needed, reducing computation complexity. For example, if the user wants only 2 decimal places, there’s no need to calculate 10 or more places, saving gas costs.
the user freindly access the avg value at till what he want a answer
@Kushal997-das once go through this pr