The last digit (in base 10) of a number is i % 10.Īnd to obtain the rune for a digit (which is in the range of 0.9), we can simply add '0' to it, and convert it to string.Last question to solve for this is how to "list" the digits of a number. We can obtain the base-10 string representation by concatenating the digits (as string values) of the number. The solution builds the "complete" numbers from digits. I don't think it is worth the hassle and the loss of readability, but actually you can define string constants holding increasing decimal numbers using iota. So the result will be a string holding a single rune, whose value (the Unicode codepoint) is the source number.Īlso calling "converter" functions such as strconv.Itoa() or fmt.Sprint() is out of the question, as calling those functions cannot be part of a constant expression, so the result could only be used in a variable declaration (not to mention we couldn't use iota, it's only allowed in constant declarations). Unfortunately for us, a simple type conversion from integer to string will not yield the base-10 representation of the numerical value, but:Ĭonverting a signed or unsigned integer value to a string type yields a string containing the UTF-8 representation of the integer. This way must be a constant expression, else we can't use it in a constant declaration. If we want string constants, we need to find a way to convert an integer to its base-10 string representation. Within a constant declaration, the predeclared identifier iota represents successive untyped integer constants.
0 Comments
Leave a Reply. |