Skip to content

Common conversions#

The following examples show how to convert between strings, integers, floating point numbers, characters (integer code points), and fixed point numbers in Toit.

Some of these require the import of the fixed_point library. For additional information on escape characters, see here.

import fixed_point show FixedPoint

main:
  str := "123"
  // Convert a string to an integer (base 10).
  i := int.parse str // i is of type int.
  print i is int  // => true
  print i  // => 123

  // Convert a string to an integer (base 16).
  // As of 2020-12-10 only base 10 and 16 are supported.
  i2/int    := int.parse --radix=16 "A0"
  print i2 is int  // => true
  print i2  // => 160

  // Convert a number to a string (direct conversion)
  s2/string := "$i"
  print s2 + " fish"  // => "123 fish".

  // Also convert a number to a string (string interpolation)
  s3/string := i.stringify
  print s3 + " fish"  // => "123 fish".

  // Convert a number to a string, base 16.
  // Format specifiers like %x are well known from printf which
  // exists in many languages.  In Toit they are placed in front
  // of the value in a string interpolation like this.
  s4/string := "$(%x i2)"
  print s4 + " fish"  // => "a0 fish".

  // Get a rune (unicode code point) from a string.  This gets the integer
  // representing the character at the position 0.
  u/int := "x"[0]
  print u  // => 120 because the Unicode value of 'x' is 120.

  // Gets the integer representing the Euro sign at position 2.
  u2/int := "24€"[2]
  print u2  // => 8364 because the Unicode value of the Euro sign is 8364.

  // Convert a unicode value to a string.
  print (string.from_rune '\u20ac') // => the Euro sign, €.

  // Also convert a unicode value to a string. Here we are also
  // using a format specifier inspired by printf. In Toit the
  // %c format works for all Unicode values, but is better for use // with strings with several elements.
  s5/string := "a string with $(%c u2) in the middle"
  print s5  // => the Euro sign, €.

  // This is not really a conversion, just a different way to write
  // strings containing special characters.  For two-digit runes
  // using \x, the curly braces are optional.
  s6/string := "S\xf8en s\x{e5} s\u{00e6}r ud."
  print s6  // => "Søen så sær ud".

  // For four-digit values you can either use \x with curly braces or \u.
  s7/string := "\u{20ac} \u20ac \x{20ac}"
  print s7  // => "€ € €", which has hexadecimal code point 0x20ac.

  // The \u version can also be used with curly braces.
  // It is in fact necessary for 5-digit Unicode runes.
  s8/string := "\u{1f648}"
  print s8  // => the see-no-evil-monkey emoji, which has hexadecimal code point 0x1f648.

  // Convert a string to a floating point number.
  f/float := float.parse "1.5"
  print f  // => 1.5.

  // Convert a floating point number to a string.
  s9/string := "$f"
  print s9  // => "1.5".

  //Also convert a floating point number to a string.
  print f.stringify

  // We can specify the precision after the decimal point using the %f format
  // specifier.
  PI ::= 3.1415926536
  s10/string := "$(%0.3f 1.2) $(%0.2f PI)"
  print s10  // => "1.200 3.14"
  //or
  print (f.stringify 3)
  print (PI.stringify 2)

  // Convert an integer to a floating point number
  f2/float := i.to_float
  print f2/2  // => 61.5, half of 123.

  // Convert a floating point number to an integer, rounding towards zero.
  i3/int := (61.9).to_int
  print i3  // => 61, rounded towards zero from 61.9.
  i4/int := (-61.9).to_int
  print i4  // => -61, rounded towards zero from -61.9

  // Convert a floating point number to an integer, rounding to nearest.
  i5/int := (61.9).round
  print i5  // => 62, rounded to nearest integer.
  i6/int := (-61.9).round
  print i6  // => -62, rounded to nearest integer.

  // Convert a floating point number to a fixed point number.
  fix/FixedPoint := FixedPoint 4.0 / 3.0
  print fix  // => 1.33 because precisions default to two decimal places.

  fix2/FixedPoint := FixedPoint --decimals=3 PI
  print fix2  // => 3.142 which is pi, rounded to three places.

  // Convert a fixed point number to a floating point number.
  // The long number is the consequence of working with floating-point numbers.
  print fix2.to_float  // => 3.1419999999999999041.

  // Convert a fixed point number to an integer, rounding towards zero.
  print fix2.to_int  // => 3, rounding towards zero.
  print (-fix2).to_int  // => 3, rounding towards zero.

  // Convert a string to a fixed point number.
  fix3 := FixedPoint "3.1415"
  print fix3  // => 3.1415 because the precision is inferred from the string.

  fix4 := FixedPoint --decimals=2 "3.1415"
  print fix4  // => 3.14 because the precision was given explicitly.

  // Convert a fixed point number to a string.
  s11/string := "$fix4"
  print s11  // => "3.14"
  // or
  print s11.stringify // => "3.14"