Category talk:Wren-big: Difference between revisions

→‎Source code: Added zero fill option to BigRat.toDecimal method.
(→‎Source code: Bug fix.)
(→‎Source code: Added zero fill option to BigRat.toDecimal method.)
Line 1,532:
// If 'rounded' is true, the value is rounded to that number of places with halves
// being rounded away from zero. Otherwise the value is truncated to that number of places.
// If 'zfill' is true, any unfilled decimal places are filled with zeros.
toDecimal(digits, rounded, zfill) {
if (!(digits is Num && digits.isInteger && digits >= 0)) {
Fiber.abort("ArgumentDigits must be a non-negative integer")
}
if (rounded.type != Bool) Fiber.abort("Rounded must be true or false.")
if (zfill.type != Bool) Fiber.abort("Zfill must be true or false.")
var qr = _n.divMod(_d)
var intPart = qr[0].toString
Line 1,558 ⟶ 1,561:
}
if (digits < 1) decPart = ""
if (decPart == "") return intPart + (zfill ? "." + ("0" * digits) : "")
return intPart + "." + decPart + (zfill ? ("0" * (digits - decPart.count)) : "")
}
 
// Convenience versions of the above which use default values for onesome or bothall parameters.
toDecimal(digits, rounded) { toDecimal(digits, truerounded, false) } // alwaysnever roundedtrailing zeros
toDecimal(digits) { toDecimal(14digits, true, false) } // 14always digitsrounded, alwaysnever roundedtrailing zeros
toDecimal { toDecimal(14, true, false) } // 14 digits, always rounded, never trailing zeros
 
// Returns a string represenation of this instance in the form "i_n/d" where 'i' is an integer.
9,485

edits