Skip to content

Commit

Permalink
Update avg.sol
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Tanisha0708 authored Nov 4, 2024
1 parent c0ed1bb commit 2a27c59
Showing 1 changed file with 58 additions and 7 deletions.
65 changes: 58 additions & 7 deletions Blockchain Development/Basic/Arithmetic/Avg/avg.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,63 @@
pragma solidity 0.8.0;

contract Avg {
// Average of three numbers
int public avg ;
// Average of three numbers with precision handling
int public avg;

function getAvg( int num1 , int num2, int num3 ) public returns( int ){
int sum = num1 + num2 + num3 ;
avg = sum / 3 ;
return avg ;
function getAvg(int num1, int num2, int num3, uint decimals) public returns (int) {
int sum = num1 + num2 + num3;

// Calculate average with desired precision
// Multiply by 10^decimals to get the required precision
int precisionMultiplier = int(10 ** decimals);
avg = (sum * precisionMultiplier) / 3;

return avg;
}

function getFormattedAvg(uint decimals) public view returns (string memory) {
// Convert the average to string with the specified number of decimal places
int integerPart = avg / int(10 ** decimals);
int fractionalPart = avg % int(10 ** decimals);

// Format as a string
return string(abi.encodePacked(
intToString(integerPart),
".",
uintToString(uint(fractionalPart), decimals)
));
}

// Helper function to convert integer part to string
function intToString(int _i) internal pure returns (string memory) {
if (_i == 0) return "0";
bool negative = _i < 0;
uint i = uint(negative ? -_i : _i);
uint j = i;
uint length;
while (j != 0) {
length++;
j /= 10;
}
bytes memory bstr = new bytes(length + (negative ? 1 : 0));
uint k = bstr.length;
while (i != 0) {
bstr[--k] = bytes1(uint8(48 + i % 10));
i /= 10;
}
if (negative) {
bstr[0] = '-';
}
return string(bstr);
}

// Helper function to convert fractional part to string with fixed decimal places
function uintToString(uint _i, uint decimals) internal pure returns (string memory) {
bytes memory bstr = new bytes(decimals);
for (uint k = decimals; k > 0; k--) {
bstr[k - 1] = bytes1(uint8(48 + _i % 10));
_i /= 10;
}
return string(bstr);
}
}
}

0 comments on commit 2a27c59

Please sign in to comment.