GithubHelp home page GithubHelp logo

fix-point-solidity-lib's Introduction

Fix point solidity library

Solidity library for converting uint256/int256 numbers to string and vice versa.

Very convenient for running tests in foundry.

Install

run the install:

forge install elawbek/fix-point-solidity-lib

then, add this to your remappings.txt file:

fp/lib/=lib/fix-point-solidity-lib/src/

Usage

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import "forge-std/Test.sol";
import {FixPointLib} from "fp/lib/FixPointLib.sol";

contract FixMathTest is Test {
    using FixPointLib for uint256;
    using FixPointLib for int256;
    using FixPointLib for string;

    function testUint() external {
        string memory str = "0.123";

        console2.log("String initial value: %s", str);

        uint256 value = str.stringToUint(18);
        console2.log("Converted initial value to uint256: %s", value);

        value += 1e18;
        str = value.uintToString(18);
        console2.log("String value after change: %s", str);
    }

    function testInt() external {
        string memory str = "-42.00320100";

        console2.log("String initial value: %s", str);

        int256 value = str.stringToInt(18);

        console2.log("Converted initial value to int256: %s", value);

        value -= 1e18;
        str = value.intToString(18);
        console2.log("String value after change: %s", str);
    }
}

after running the command

forge test --mp test/FixMathTest.sol -vvv

you will see

[PASS] testInt() (gas: 14407)
Logs:
  String initial value: -42.00320100
  Converted initial value to uint256: -42003201000000000000
  String value after change: -43.003201

[PASS] testUint() (gas: 12948)
Logs:
  String initial value: 0.123
  Converted initial value to uint256: 123000000000000000
  String value after change: 1.123

fix-point-solidity-lib's People

Contributors

elawbek avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.