Function float

fn float<Input, Output, Error>(input: &mut Input) -> Result<Output, Error>
where
    Input: StreamIsPartial + Stream + Compare<Caseless<&'static str>> + Compare<char> + AsBStr,
    <Input as Stream>::Slice: ParseSlice<Output>,
    <Input as Stream>::Token: AsChar + Clone,
    <Input as Stream>::IterOffsets: Clone,
    Error: ParserError<Input>

Recognizes floating point number in text format and returns a f32 or f64.

Complete version: Can parse until the end of input.

[Partial version][crate::_topic::partial]: Will return Err(winnow::error::ErrMode::Incomplete(_)) if there is not enough data.

Effective Signature

Assuming you are parsing a &str [Stream] into an f64:

# use winnow::prelude::*;;
pub fn float(input: &mut &str) -> ModalResult<f64>
# {
#     winnow::ascii::float.parse_next(input)
# }

Example

# use winnow::prelude::*;
# use winnow::error::Needed::Size;
use winnow::ascii::float;

fn parser<'s>(s: &mut &'s str) -> ModalResult<f64> {
  float(s)
}

assert_eq!(parser.parse_peek("11e-1"), Ok(("", 1.1)));
assert_eq!(parser.parse_peek("123E-02"), Ok(("", 1.23)));
assert_eq!(parser.parse_peek("123K-01"), Ok(("K-01", 123.0)));
assert!(parser.parse_peek("abc").is_err());
# use winnow::prelude::*;
# use winnow::{error::ErrMode, error::Needed};
# use winnow::error::Needed::Size;
# use winnow::Partial;
use winnow::ascii::float;

fn parser<'s>(s: &mut Partial<&'s str>) -> ModalResult<f64> {
  float(s)
}

assert_eq!(parser.parse_peek(Partial::new("11e-1 ")), Ok((Partial::new(" "), 1.1)));
assert_eq!(parser.parse_peek(Partial::new("11e-1")), Err(ErrMode::Incomplete(Needed::new(1))));
assert_eq!(parser.parse_peek(Partial::new("123E-02")), Err(ErrMode::Incomplete(Needed::new(1))));
assert_eq!(parser.parse_peek(Partial::new("123K-01")), Ok((Partial::new("K-01"), 123.0)));
assert!(parser.parse_peek(Partial::new("abc")).is_err());