Function in conv coil-stdlib

parse_float

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

Parses a decimal float such as "3.5", "-0.25", "1e3" or "42".

Surrounding ASCII whitespace, a sign and an exponent (e or E) are accepted.

Parameters#

NameType
sstring

Returns#

Result<float, string>

Errors#

Err("invalid float") when s is not a number.

Examples#

use conv::{parse_float};
use tests::assert;
assert!((parse_float("2.5") ?? 0.0) == 2.5);
assert!((parse_float("1e3") ?? 0.0) == 1000.0);
assert!(parse_float("abc").is_err());

See also#

  • 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_int_radix: Parses an integer written in base radix, which must be 10 or 16.
  • parse_int: Parses a decimal integer, such as "42", " -7 " or "+3".
↑ ↓ navigate ↵ open