Decimal.fromStringImpl

struct Decimal(size_t maxSize64)
scope @trusted pure @nogc nothrow
bool
fromStringImpl
(
C
bool allowSpecialValues = true
bool allowDotOnBounds = true
bool allowDExponent = true
bool allowStartingPlus = true
bool allowUnderscores = true
bool allowLeadingZeros = true
bool allowExponent = true
bool checkEmpty = true
)
(
scope const(C)[] str
,,
int exponentShift = 0
)
if (
isSomeChar!C
)
if (
maxSize64 &&
maxSize64 <= ushort.max
)

Return Value

Type: bool

false in case of overflow or incorrect string.

Examples

1 import mir.conv: to;
2 Decimal!3 decimal;
3 DecimalExponentKey key;
4 
5 // Check precise percentate parsing
6 assert(decimal.fromStringImpl("71.7", key, -2));
7 assert(key == DecimalExponentKey.dot);
8 // The result is exact value instead of 0.7170000000000001 = 71.7 / 100
9 assert(cast(double) decimal == 0.717);
10 
11 assert(decimal.fromStringImpl("+0.334e-5"w, key));
12 assert(key == DecimalExponentKey.e);
13 assert(cast(double) decimal == 0.334e-5);
14 
15 assert(decimal.fromStringImpl("100_000_000"w, key));
16 assert(key == DecimalExponentKey.none);
17 assert(cast(double) decimal == 1e8);
18 
19 assert(decimal.fromStringImpl("-334D-5"d, key));
20 assert(key == DecimalExponentKey.D);
21 assert(cast(double) decimal == -334e-5);
22 
23 assert(decimal.fromStringImpl("2482734692817364218734682973648217364981273648923423", key));
24 assert(key == DecimalExponentKey.none);
25 assert(cast(double) decimal == 2482734692817364218734682973648217364981273648923423.0);
26 
27 assert(decimal.fromStringImpl(".023", key));
28 assert(key == DecimalExponentKey.dot);
29 assert(cast(double) decimal == .023);
30 
31 assert(decimal.fromStringImpl("0E100", key));
32 assert(key == DecimalExponentKey.E);
33 assert(cast(double) decimal == 0);
34 
35 foreach (str; ["-nan", "-NaN", "-NAN"])
36 {
37     assert(decimal.fromStringImpl(str, key));
38     assert(decimal.coefficient.length > 0);
39     assert(decimal.exponent == decimal.exponent.max);
40     assert(decimal.coefficient.sign);
41     assert(key == DecimalExponentKey.nan);
42     assert(cast(double) decimal != cast(double) decimal);
43 }
44 
45 foreach (str; ["inf", "Inf", "INF"])
46 {
47     assert(decimal.fromStringImpl(str, key));
48     assert(decimal.coefficient.length == 0);
49     assert(decimal.exponent == decimal.exponent.max);
50     assert(key == DecimalExponentKey.infinity);
51     assert(cast(double) decimal == double.infinity);
52 }
53 
54 assert(decimal.fromStringImpl("-inf", key));
55 assert(decimal.coefficient.length == 0);
56 assert(decimal.exponent == decimal.exponent.max);
57 assert(key == DecimalExponentKey.infinity);
58 assert(cast(double) decimal == -double.infinity);
59 
60 assert(!decimal.fromStringImpl("3.3.4", key));
61 assert(!decimal.fromStringImpl("3.4.", key));
62 assert(decimal.fromStringImpl("4.", key));
63 assert(!decimal.fromStringImpl(".", key));
64 assert(decimal.fromStringImpl("0.", key));
65 assert(decimal.fromStringImpl("00", key));
66 assert(!decimal.fromStringImpl("0d", key));

Meta