Standard library · Basics

conv

Converting numbers and booleans to text and back, including Persian digits.

import conv
On this page
  1. Functions

Text arriving from a user, a file or the network is always a string; to do arithmetic with it you have to turn it into a number, and to show the result you turn it back into text. The conv package does both halves of that.

The parsing functions also understand Persian digits (۰۱۲۳۴۵۶۷۸۹) and Arabic ones (٠١٢٣٤٥٦٧٨٩), so a number typed on a Persian keyboard is read without any fuss. These functions never fail: they read from the start of the string for as long as it makes sense as a number and ignore the rest, and return zero when they find no digits at all.

Functions

FormatUint

func FormatUint(x: u64): str
Turns an unsigned 64-bit number into text.
conv-FormatInt.salam
import conv

func main:
    println conv.FormatInt(-1200)
    println conv.FormatUint(18446744073709551615 as u64)
    println conv.FormatBool(false)
    println conv.FormatHex(4096 as u64)
end
Output-1200 18446744073709551615 false 1000

FormatInt

func FormatInt(n: i64): str
Turns a 64-bit whole number into text, with a minus sign for negative values.

The example for this item is shown with the ones above: conv-FormatInt.salam

FormatBool

func FormatBool(b: bool): str
Turns true into "true" and false into "false".

The example for this item is shown with the ones above: conv-FormatInt.salam

ParseBool

func ParseBool(s: str): bool
Turns a string into a boolean. "true", "1" and "yes", with any surrounding spaces, count as true, and anything else as false.
conv-ParseInt.salam
import conv

func main:
    println conv.ParseInt("  -۲۷ "), conv.ParseInt("-27"), conv.ParseInt("12abc"), conv.ParseInt("no digits")
    println conv.Atoi("+15")
    println conv.ParseHex("0xff"), conv.ParseHex("1A")
    println conv.ParseBool("yes"), conv.ParseBool(" true "), conv.ParseBool("maybe")
end
Output-27 -27 12 0 15 255 26 true true false

ParseInt

func ParseInt(s: str): i64
Reads a whole number from the start of a string. Leading spaces are ignored and a + or - sign is accepted. Persian, Arabic and English digits are all understood, and reading stops at the first character that is not a digit.

The example for this item is shown with the ones above: conv-ParseInt.salam

Atoi

func Atoi(s: str): int
The same as ParseInt, but returns an int, which is 32 bits.

The example for this item is shown with the ones above: conv-ParseInt.salam

ParseFloat

func ParseFloat(s: str): f64
Reads a decimal number from a string. Scientific notation such as 2.5e3, and Persian digits with the Persian decimal separator (۳٫۷۵), are both accepted.
conv-ParseFloat.salam
import conv

func main:
    x := conv.ParseFloat("۳٫۷۵")
    println x * 2
    println conv.FormatFloat(0.1 + 0.2)
    println conv.FormatFloatShort(0.1 + 0.2)
    println conv.FormatFloatPrec(2.0 / 3.0, 3)
    nan := conv.ParseFloat("nan")
    println conv.IsNaNF(nan), conv.IsNaNF(x)
end
Output7.5 0.30000000000000004 0.3 0.667 true false

IsNaNF

func IsNaNF(x: f64): bool
Returns true when the number is "not a number" (NaN), for instance the result of parsing "nan". No comparison with == can detect this case, because NaN is not even equal to itself.

The example for this item is shown with the ones above: conv-ParseFloat.salam

FormatFloat

func FormatFloat(x: f64): str
Turns a decimal number into text with every digit needed to read exactly the same number back.

The example for this item is shown with the ones above: conv-ParseFloat.salam

FormatFloatShort

func FormatFloatShort(x: f64): str
Turns a decimal number into the shortest readable form; 0.1 + 0.2, for example, comes out as 0.3.

The example for this item is shown with the ones above: conv-ParseFloat.salam

FormatFloatPrec

func FormatFloatPrec(x: f64, prec: int): str
Turns a decimal number into text with the number of decimal places you ask for, rounded.

The example for this item is shown with the ones above: conv-ParseFloat.salam

FormatHex

func FormatHex(x: u64): str
Writes the number in base sixteen in lower case, with no 0x prefix.

The example for this item is shown with the ones above: conv-FormatInt.salam

ParseHex

func ParseHex(s: str): i64
Reads a base sixteen number. The 0x prefix is optional and both upper and lower case letters are accepted.

The example for this item is shown with the ones above: conv-ParseInt.salam