Category talk:Wren-fmt: Difference between revisions

→‎Source code: Added support for signing floating point numbers.
(Bug fix.)
(→‎Source code: Added support for signing floating point numbers.)
Line 415:
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) }
 
// 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,
Line 476 ⟶ 489:
static gz(w, n) { gz(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 gc(w, n) { gc(w, n, precision) }
static hc(w, n) { hc(w, n, precision) }
 
// Private worker method which calls a 'short name' method and returns its result.
Line 514 ⟶ 530:
(fn == "gz") ? gz(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 == "dm") ? dm(w, v) :
Line 528 ⟶ 547:
// 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'
// 'p' is needed for 'e', 'E', 'f', 'g', 'h', 'z', 'fz', 'gz', 'hz', 'fcfp', 'gcgp'
// 'hp', 'fc', 'gc' or 'hc' but is ignored otherwise.
// The resulting strings are then joined together using the separator 'sep'.
// having first applied the 'q' method, with parameter 'cc', to each of them.
Line 586 ⟶ 605:
// 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:
// + always prints a + or - sign ('dp', method'fp', 'gp' or 'hp' methods)
// (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)
Line 701 ⟶ 720:
fn = "dc"
}
} else if ((fn == "f" || fn == "g" || fn == "h") && plus) {
fn = fn + "p"
} else if ((fn == "r" || fn == "s" || fn == "i" || fn == "f" ||
fn == "g" || fn == "h") && comma) {
9,485

edits