Skip to content

How to convert from unsigned to signed? #47

Closed Answered by PaulRBerg
joaomontenegro asked this question in Q&A
Discussion options

You must be logged in to vote

PRBMath comes with a multitude of castingand conversion functions. To convert from UD60x18 to SD59x18, you would do something like this:

pragma solidity >=0.8.13;

import "@prb/math/UD60x18.sol";

contract YourContract {
    function converUD60x18IntoSD59x18(UD60x18 x) external pure returns (SD59x18 result) {
        uint256 xUint = UD60x18.unwrap(x);
        require(xUint <= uint256(type(int256).max), "int256 overflow");
        int256 xInt = int256(xUint);
        result = SD59x18.wrap(xInt);
    }
}

I don't think that offering a cross-type convertor would be helpful, since the vast majority of PRBMath users import either the signed or the unsigned flavor (users rarely import both at th…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by PaulRBerg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants