This section explains how to convert uint to string in Solidity.
Syntax
Since string manipulation is difficult in plain Solidity, a library (OpenZeppelin) is used.
First, in order to be able to use OpenZeppelin, import it as follows.
import "@openzeppelin/contracts/utils/Strings.sol";
Convert uint to string using OpenZeppelin's Strings.toString() method.
Strings.toString(number[uint type])
Sample Code
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
import "@openzeppelin/contracts/utils/Strings.sol";
contract Test {
function uintToString(uint num) public pure returns(string memory) {
return Strings.toString(num); // convert uint to string
}
}
Solidity is a dedicated blockchain language, which means that unlike other programming languages, it is not good at manipulating strings. Therefore, you should either create your own library (write your own dedicated conversion process) or use external libraries such as OpenZeppelin.