Nim example - Convert String to/from the Float

This tutorials explains about convert String to float and float to string in Nim language with examples.

float contains decimals with a number. If an string contains invalid numeric values, It throws ValueError.

How to convert String to Float in Nim

parseFloat in strutils module convert given string to integer. It throws an ValueError if string is invalid number.

Here is an example

import strutils
const value = parseFloat("12.12")
echo typeof(value) # float
echo value # 12.12

How to convert Float to String in Nim

Multiple ways we can convert int to string in nim.

  • use $ operator

$ is a toString() operator that converts any type to String.

Here is an example

import strutils
const number=11.123
let str = $number

echo typeof(str) # string
echo str # 11.123