Commatizing numbers: Difference between revisions
Content added Content deleted
m (→{{header|D}}) |
|||
Line 250: | Line 250: | ||
// The first number field begins with zero then no digit, or non-zero. |
// The first number field begins with zero then no digit, or non-zero. |
||
auto numField = |
auto numField = ctRegex!("0(?![0-9])|[1-9][0-9]*"); |
||
auto matchNum = matchFirst(scanSpan, numField); |
auto matchNum = matchFirst(scanSpan, numField); |
||
Line 257: | Line 257: | ||
// Pass only a point and capture a decimal fractional field, if any. |
// Pass only a point and capture a decimal fractional field, if any. |
||
auto decField = |
auto decField = ctRegex!("(?<=^\\.)[0-9]+"); |
||
static if (smart) { |
static if (smart) { |
||
Line 276: | Line 276: | ||
} |
} |
||
⚫ | |||
// There may be special prefixed formats that use different separators. |
// There may be special prefixed formats that use different separators. |
||
// Any format with a longer prefix should override a shorter one. |
// Any format with a longer prefix should override a shorter one. |
||
Tuple!(string, string)[] pairs; |
Tuple!(string, string)[] pairs; |
||
⚫ | |||
foreach (pair; specials.byPair) { |
|||
pairs ~= pair; |
|||
} |
} |
||
std.algorithm.sort!("a[0].length < b[0].length")(pairs); |
std.algorithm.sort!("a[0].length < b[0].length")(pairs); |
||
foreach (symbol; pairs) { |
|||
auto preAnyDigit = matchNum.pre.stripRight('0'); |
|||
foreach (symbol; pairs) { |
|||
if (preAnyDigit.length >= symbol[0].length && |
|||
symbol[0] == preAnyDigit[$ - symbol[0].length .. $]) |
|||
ins = symbol[1]; |
|||
} |
|||
} |
} |
||
Line 307: | Line 310: | ||
assert("1000 2.3000".commatize == "1,000 2.3000"); |
assert("1000 2.3000".commatize == "1,000 2.3000"); |
||
assert("0001123.456789".commatize == "0001,123.456,789"); |
assert("0001123.456789".commatize == "0001,123.456,789"); |
||
assert("Z$01000".commatize(0, 3, ",", ["Z$":" "]) == "Z$01 000"); |
|||
}</lang> |
}</lang> |
||
{{out}} |
{{out}} |