Function in conv coil-stdlib

parse_int

fn parse_int(string s) -> Result<int, string>
Import
use conv::{parse_int};
Source: src/conv.hy

Parses a decimal integer, such as "42", " -7 " or "+3".

Parameters#

NameType
sstring

Returns#

Result<int, string>

Errors#

Err("invalid integer") when s is empty or has a character that is not a digit. See parse_int_radix for the overflow caveat.

Examples#

use conv::{parse_int};
use tests::assert;
assert!((parse_int(" 42 ") ?? 0) == 42);
let bad = match parse_int("4x2") {
    Result::Err(e) => e,
    Result::Ok(_) => "",
};
assert!(bad == "invalid integer");

See also#

  • conv::parse_int_radix
  • int_to_dec: Formats n in decimal; the same as format("%i", n).
  • int_to_hex: Formats n in lowercase hexadecimal without a prefix; a negative n gets a leading -.
  • int_to_bin: Formats n in binary without a prefix; a negative n gets a leading -.
  • parse_float: Parses a decimal float such as "3.5", "-0.25", "1e3" or "42".
↑ ↓ navigate ↵ open