Created
October 20, 2022 13:43
-
-
Save TheRolfFR/f07155f06d899ec8c71b79ec6d875bdf to your computer and use it in GitHub Desktop.
C++ convert int to string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
std::string from_int(int value) { | |
int tmp = value; | |
std::string valueAsString = tmp < 0 ? "-" : ""; | |
size_t pos = valueAsString.length(); | |
if(tmp < 0) tmp = -tmp; | |
else if(tmp == 0) valueAsString = "0"; | |
while(tmp != 0) { | |
int digit = tmp%10; | |
valueAsString.insert(pos,1,'0' + digit); | |
tmp/=10; | |
} | |
return valueAsString; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment