How to convert an int to a string in Nim

There is an intToStr method that can convert an int to a string:

import strutils
let myNum = 4
let s = intToStr(myNum)
echo s

Running this yields:

4

To convert a very small or very large value, you can use Nim's "toString" operator, $:

let num = 54322348923482347
let numStr = $num
echo numStr[1]

Running this yields:

4

If you want to print the value in a formatted string, you can use the strformat library:

import strformat
let myNum = 4573589373453485
let s = fmt"Value: {myNum}"
echo s

Running this yields:

Value: 4573589373453485

Comments

Leave a comment

What color are green eyes? (spam prevention)
Submit
Code under MIT License unless otherwise indicated.
© 2020, Downranked, LLC.