Best way to initialize a fractional fixed point value. #45
-
What's the best way to initialize a uint256 with a fractional number? Pseudocode of an example of what I am trying to achieve: |
Beta Was this translation helpful? Give feedback.
Answered by
PaulRBerg
Aug 13, 2021
Replies: 1 comment
-
Hey! There are multiple ways to do this, as there is a multitude of conversion function shipped with PRBmath. Scientific Notation// 0.5
UD60x18 foo = ud(0.5e18);
UD60x18 foo = ud60x18(0.5e18);
UD60x18 foo = wrap(0.5e18); "To" Conversion FunctionThis only works for whole numbers greater than or equal to 1e18. See the NatSpec comments for the // 50
UD60x18 foo = toUD60x18(50); |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
PaulRBerg
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey! There are multiple ways to do this, as there is a multitude of conversion function shipped with PRBmath.
Scientific Notation
"To" Conversion Function
This only works for whole numbers greater than or equal to 1e18. See the NatSpec comments for the
toUD60x18
for more details on how this works.