Category talk:Wren-fmt: Difference between revisions
Content added Content deleted
(Bug fix.) |
(→Source code: Added support for signing floating point numbers.) |
||
Line 415: | Line 415: | ||
static gz(w, n, p) { (w >= 0) ? zfill(w, g(w, n, p).trimStart()) : g(w, n, p) } |
static gz(w, n, p) { (w >= 0) ? zfill(w, g(w, n, p).trimStart()) : g(w, n, p) } |
||
static hz(w, n, p) { (w >= 0) ? zfill(w, h(w, n, p).trimStart()) : h(w, n, p) } |
static hz(w, n, p) { (w >= 0) ? zfill(w, h(w, n, p).trimStart()) : h(w, n, p) } |
||
// As above but prepends non-negative numbers with a '+' sign. |
|||
static fp(w, n, p) { signFloat_("f", w, n, p) } |
|||
static gp(w, n, p) { signFloat_("g", w, n, p) } |
|||
static hp(w, n, p) { signFloat_("h", w, n, p) } |
|||
// Private helper method for signing floating point numbers. |
|||
static signFloat_(fn, w, n, p) { |
|||
var fmt = "$%(w).%(p)%(fn)" |
|||
if (n < 0) return swrite(fmt, n) |
|||
if (n > 0) return swrite(fmt, -n).replace("-", "+") |
|||
return swrite(fmt, -1).replace("-1", "+0") |
|||
} |
|||
// Formats the integer part of 'n' in commatized form, space padded, |
// Formats the integer part of 'n' in commatized form, space padded, |
||
Line 476: | Line 489: | ||
static gz(w, n) { gz(w, n, precision) } |
static gz(w, n) { gz(w, n, precision) } |
||
static hz(w, n) { hz(w, n, precision) } |
static hz(w, n) { hz(w, n, precision) } |
||
static fp(w, n) { fp(w, n, precision) } |
|||
static gp(w, n) { gp(w, n, precision) } |
|||
static hp(w, n) { hp(w, n, precision) } |
|||
static fc(w, n) { fc(w, n, precision) } |
static fc(w, n) { fc(w, n, precision) } |
||
static gc(w, n) { gc(w, n, precision) } |
static gc(w, n) { gc(w, n, precision) } |
||
static hc(w, n) { hc(w, n, precision) } |
static hc(w, n) { hc(w, n, precision) } |
||
// Private worker method which calls a 'short name' method and returns its result. |
// Private worker method which calls a 'short name' method and returns its result. |
||
Line 514: | Line 530: | ||
(fn == "gz") ? gz(w, v, p) : |
(fn == "gz") ? gz(w, v, p) : |
||
(fn == "hz") ? hz(w, v, p) : |
(fn == "hz") ? hz(w, v, p) : |
||
(fn == "fp") ? fp(w, v, p) : |
|||
(fn == "gp") ? gp(w, v, p) : |
|||
(fn == "hp") ? hp(w, v, p) : |
|||
(fn == "dp") ? dp(w, v) : |
(fn == "dp") ? dp(w, v) : |
||
(fn == "dm") ? dm(w, v) : |
(fn == "dm") ? dm(w, v) : |
||
Line 528: | Line 547: | ||
// The method to be applied is specified (as a string) in 'fn'. |
// The method to be applied is specified (as a string) in 'fn'. |
||
// The parameters to be passed to the method are specified in 'w' and 'p' |
// The parameters to be passed to the method are specified in 'w' and 'p' |
||
// 'p' is needed for 'e', 'E', 'f', 'g', 'h', 'z', 'fz', 'gz', 'hz', ' |
// 'p' is needed for 'e', 'E', 'f', 'g', 'h', 'z', 'fz', 'gz', 'hz', 'fp', 'gp' |
||
// or 'hc' but is ignored otherwise. |
// 'hp', 'fc', 'gc' or 'hc' but is ignored otherwise. |
||
// The resulting strings are then joined together using the separator 'sep'. |
// The resulting strings are then joined together using the separator 'sep'. |
||
// having first applied the 'q' method, with parameter 'cc', to each of them. |
// having first applied the 'q' method, with parameter 'cc', to each of them. |
||
Line 586: | Line 605: | ||
// a, b, c, d, e, E, f, g, h, i, k, m, n, o, q, r, s, t, x, X or z. |
// a, b, c, d, e, E, f, g, h, i, k, m, n, o, q, r, s, t, x, X or z. |
||
// If present, the flag (there can only be one) must be one of the following: |
// If present, the flag (there can only be one) must be one of the following: |
||
// + always prints a + or - sign ('dp' |
// + always prints a + or - sign ('dp', 'fp', 'gp' or 'hp' methods) |
||
// (space) leaves a space for the sign but only prints minus ('dm' method) |
// (space) leaves a space for the sign but only prints minus ('dm' method) |
||
// , commatizes the following number ('dc', 'rc', 'sc', 'ic', 'fc', 'gc' or 'hc' methods) |
// , commatizes the following number ('dc', 'rc', 'sc', 'ic', 'fc', 'gc' or 'hc' methods) |
||
Line 701: | Line 720: | ||
fn = "dc" |
fn = "dc" |
||
} |
} |
||
} else if ((fn == "f" || fn == "g" || fn == "h") && plus) { |
|||
fn = fn + "p" |
|||
} else if ((fn == "r" || fn == "s" || fn == "i" || fn == "f" || |
} else if ((fn == "r" || fn == "s" || fn == "i" || fn == "f" || |
||
fn == "g" || fn == "h") && comma) { |
fn == "g" || fn == "h") && comma) { |