conv
Converting numbers and booleans to text and back, including Persian digits.
import conv
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): strimport conv
func main:
println conv.FormatInt(-1200)
println conv.FormatUint(18446744073709551615 as u64)
println conv.FormatBool(false)
println conv.FormatHex(4096 as u64)
endFormatInt
func FormatInt(n: i64): strThe example for this item is shown with the ones above: conv-FormatInt.salam
FormatBool
func FormatBool(b: bool): strtrue 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"true", "1" and "yes", with any surrounding spaces, count as true, and anything else as false.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")
endParseInt
func ParseInt(s: str): i64+ 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): intParseInt, 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): f642.5e3, and Persian digits with the Persian decimal separator (۳٫۷۵), are both accepted.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)
endIsNaNF
func IsNaNF(x: f64): bool"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): strThe example for this item is shown with the ones above: conv-ParseFloat.salam
FormatFloatShort
func FormatFloatShort(x: f64): str0.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): strThe example for this item is shown with the ones above: conv-ParseFloat.salam
FormatHex
func FormatHex(x: u64): str0x prefix.The example for this item is shown with the ones above: conv-FormatInt.salam
ParseHex
func ParseHex(s: str): i640x 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