parse

Parses a floating point number from a character range.

Parameters

Target

a floating point type

source Source

the lvalue of the range to parse

doCount

the flag for deciding to report the number of consumed characters

Return Value

Type: auto
  • A floating point number of type Target if doCount is set to No.doCount
  • A tuple containing a floating point number of·type Target and a size_t if doCount is set to Yes.doCount

Throws

A ConvException if source is empty, if no number could be parsed, or if an overflow occurred.

Examples

import std.math.operations : isClose;
import std.math.traits : isNaN, isInfinity;
import std.typecons : Flag, Yes, No;
auto str = "123.456";
assert(parse!double(str).isClose(123.456));
auto str2 = "123.456";
assert(parse!(double, string, No.doCount)(str2).isClose(123.456));
auto str3 = "123.456";
auto r = parse!(double, string, Yes.doCount)(str3);
assert(r.data.isClose(123.456));
assert(r.count == 7);
auto str4 = "-123.456";
r = parse!(double, string, Yes.doCount)(str4);
assert(r.data.isClose(-123.456));
assert(r.count == 8);
auto str5 = "+123.456";
r = parse!(double, string, Yes.doCount)(str5);
assert(r.data.isClose(123.456));
assert(r.count == 8);
auto str6 = "inf0";
r = parse!(double, string, Yes.doCount)(str6);
assert(isInfinity(r.data) && r.count == 3 && str6 == "0");
auto str7 = "-0";
auto r2 = parse!(float, string, Yes.doCount)(str7);
assert(r2.data.isClose(0.0) && r2.count == 2);
auto str8 = "nan";
auto r3 = parse!(real, string, Yes.doCount)(str8);
assert(isNaN(r3.data) && r3.count == 3);

Meta