How to convert uint to string in solidity?

31,812

Solution 1

The two post here are giving the responses :

https://ethereum.stackexchange.com/questions/10811/solidity-concatenate-uint-into-a-string

https://ethereum.stackexchange.com/questions/10932/how-to-convert-string-to-int

function uintToString(uint v) constant returns (string str) {
        uint maxlength = 100;
        bytes memory reversed = new bytes(maxlength);
        uint i = 0;
        while (v != 0) {
            uint remainder = v % 10;
            v = v / 10;
            reversed[i++] = byte(48 + remainder);
        }
        bytes memory s = new bytes(i + 1);
        for (uint j = 0; j <= i; j++) {
            s[j] = reversed[i - j];
        }
        str = string(s);
    }

Regards

Solution 2

solidity ^0.8.0
import "@openzeppelin/contracts/utils/Strings.sol";
Strings.toString(myUINT)

works for me.

https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/Strings.sol#L15-L35

Solution 3

UPDATE for Solidity 0.8.0:

The uint2str() function from https://github.com/provable-things/ethereum-api/blob/master/provableAPI_0.6.sol is now outdated, and will not work, but here is the updated code, that uses solidity 0.8.0: (there was an Overflow bug in the last version but solidity <0.8.0 ignored that as it did not affect the answer, but that now throws an error) byte was also changed to bytes1 and +,-,* and so on work like they would from the SafeMath library.

function uint2str(uint _i) internal pure returns (string memory _uintAsString) {
        if (_i == 0) {
            return "0";
        }
        uint j = _i;
        uint len;
        while (j != 0) {
            len++;
            j /= 10;
        }
        bytes memory bstr = new bytes(len);
        uint k = len;
        while (_i != 0) {
            k = k-1;
            uint8 temp = (48 + uint8(_i - _i / 10 * 10));
            bytes1 b1 = bytes1(temp);
            bstr[k] = b1;
            _i /= 10;
        }
        return string(bstr);
    }

Solution 4

Concrete_Buddhas answer does not work in solidity 0.8.0. This is a revised version:

 function uint2str(
  uint256 _i
)
  internal
  pure
  returns (string memory str)
{
  if (_i == 0)
  {
    return "0";
  }
  uint256 j = _i;
  uint256 length;
  while (j != 0)
  {
    length++;
    j /= 10;
  }
  bytes memory bstr = new bytes(length);
  uint256 k = length;
  j = _i;
  while (j != 0)
  {
    bstr[--k] = bytes1(uint8(48 + j % 10));
    j /= 10;
  }
  str = string(bstr);
}

Solution 5

The provable-things code suggested in the comments to the accepted answer worked for me, but my linter threw a warning namely: "uintToStr": Avoid assigning to function parameters. [security/no-assign-params]. The below changes the original code slightly to correct this (reassigning the parameter _i to another variable called number):

    /// @notice converts number to string
    /// @dev source: https://github.com/provable-things/ethereum-api/blob/master/oraclizeAPI_0.5.sol#L1045
    /// @param _i integer to convert
    /// @return _uintAsString
    function uintToStr(uint _i) internal pure returns (string memory _uintAsString) {
        uint number = _i;
        if (number == 0) {
            return "0";
        }
        uint j = number;
        uint len;
        while (j != 0) {
            len++;
            j /= 10;
        }
        bytes memory bstr = new bytes(len);
        uint k = len - 1;
        while (number != 0) {
            bstr[k--] = byte(uint8(48 + number % 10));
            number /= 10;
        }
        return string(bstr);
    }
Share:
31,812
Bob Zheng
Author by

Bob Zheng

Here and Now is Happiness!

Updated on February 13, 2022

Comments

  • Bob Zheng
    Bob Zheng 10 months

    In Solidity, is there a way I can convert my int to string ?

    Example:

    pragma solidity ^0.4.4;
    contract someContract {
        uint i;
        function test() pure returns (string) {
          return "Here and Now is Happiness!";
        }
        function love() pure returns(string) {
            i = i +1;
            return "I love " + functionname(i) + " persons" ;
        }
    }
    

    What is functionname?Thanks!

  • Dmitriy Vinokurov
    Dmitriy Vinokurov almost 5 years
    This variant was buggy when I tested it, solution from Oraclize github.com/oraclize/ethereum-api/blob/master/… may be better: function uint2str(uint i) internal pure returns (string){ if (i == 0) return "0"; uint j = i; uint length; while (j != 0){ length++; j /= 10; } bytes memory bstr = new bytes(length); uint k = length - 1; while (i != 0){ bstr[k--] = byte(48 + i % 10); i /= 10; } return string(bstr); }
  • Diiaablo
    Diiaablo over 2 years
    Yeah I agree that the version in the answer did not work for me, but the one provided in github.com/provable-things/ethereum-api/blob/master/… did, as pointed out by Dmitriy above here.
  • Buzz Moschetti
    Buzz Moschetti over 2 years
    Also: solc 0.7.0 does not like byte(48 + remainder). Cannot explicitly convert uint256 to bytes1.
  • Senju
    Senju almost 2 years
    This version works with solidity 0.8.1 ! Please upvote. :)
  • pandichef
    pandichef over 1 year
    Can you modify this to have a decimals parameters? e.g., if uint is 20000 and decimals is 3, then the result would be 20.
  • 0xD1x0n
    0xD1x0n 12 months
    appriciate the update
  • EdwardG
    EdwardG 11 months
    This should be the accepted answer.
  • James 10 months
    Agreed this should be accepted
  • Christophe Vidal
    Christophe Vidal 7 months
    Perfect answer. Also to concatenate strings, + doesn't work, but this shall be done with return string(abi.encodePacked("I love ", Strings.toString(i), " persons"));