Gets a string from an integer (ejemplo gratis: 123 -> "123")
Wanted to come up with my own function for this like 10 years ago but couldn't think of a way and used stringstream. Now after all the binary & bitwise stuff I've been doing, the answer easily popped into my head when the topic of getting the last digit in a number came to mind.
// get lowest digit (of base-10 format number)
int getLowestDigit(int num){
int lowest_digit = num % 10;
return lowest_digit;
}
// convert number to string (base-10 representation) in probs a super inefficient way
std::string numToString(int num){
std::string str = "";
while(num > 0){
int digi = num % 10; // current right-most digit
str += '0' + digi;
num /= 10; // move number over by one base-10 digit's worth
}
// above number string is backwards. flip it
std::string fin = "";
for(int xint = fin.length() - 1;xint > -1;xint--){
fin += str[xint];
}
return fin;
}
No comments:
Post a Comment