-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFloatDivision.sol
47 lines (43 loc) · 1.37 KB
/
FloatDivision.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.1;
import "@openzeppelin/contracts/utils/Strings.sol";
// This allows Float(Decimal) Division in solidity and returns result as string
contract FloatDivision {
using Strings for uint256;
function division(
uint256 decimalPlaces,
uint256 numerator,
uint256 denominator
)
public
pure
returns (uint256 quotient, uint256 remainder, string memory result)
{
uint256 factor = 10 ** decimalPlaces;
quotient = numerator / denominator;
bool rounding = 2 * ((numerator * factor) % denominator) >= denominator;
remainder = ((numerator * factor) / denominator) % factor;
if (rounding) {
remainder += 1;
}
result = string(
abi.encodePacked(
quotient.toString(),
".",
numToFixedLengthStr(decimalPlaces, remainder)
)
);
}
function numToFixedLengthStr(
uint256 decimalPlaces,
uint256 num
) internal pure returns (string memory result) {
bytes memory byteString;
for (uint256 i = 0; i < decimalPlaces; i++) {
uint256 remainder = num % 10;
byteString = abi.encodePacked(remainder.toString(), byteString);
num = num / 10;
}
result = string(byteString);
}
}