Decimal.fromString

Parse price number from string.

struct Decimal
static nothrow @nogc pure @safe
Nullable!Decimal
fromString
(
scope const(char)[] s
)

Parameters

s const(char)[]

target string

Return Value

Type: Nullable!Decimal

Decimal if succceeded.

Examples

import std.exception : assertThrown;
import std.range : array, repeat;

assert(Decimal.fromString(".123456") == Decimal(123456, 6));
assert(Decimal.fromString("123.456") == Decimal(123456, 3));
assert(Decimal.fromString("12345.6") == Decimal(123456, 1));
assert(Decimal.fromString("123456.") == Decimal(123456, 0));
assert(Decimal.fromString("123456") == Decimal(123456, 0));
assert(Decimal.fromString("0.001") == Decimal(1, 3));
assert(Decimal.fromString("100.000") == Decimal(100000, 3));

assert(Decimal.fromString("a123456").isNull);
assert(Decimal.fromString("123.456a").isNull);

string zeros = '0'.repeat(ubyte.max - 1).array;
assert(Decimal.fromString("." ~ zeros ~ "1") == Decimal(1, ubyte.max));
assert(Decimal.fromString("." ~ zeros ~ "01").isNull);

assert(Decimal.fromString("+123.456") == Decimal(+123456, 3));
assert(Decimal.fromString("-123.456") == Decimal(-123456, 3));
assert(Decimal.fromString("+.123456") == Decimal(123456, 6));
assert(Decimal.fromString("-.123456") == Decimal(-123456, 6));

Meta