Category talk:Wren-big: Difference between revisions

Content added Content deleted
(→‎Source code: Bug fix.)
(→‎Source code: Added zero fill option to BigRat.toDecimal method.)
Line 1,532: Line 1,532:
// If 'rounded' is true, the value is rounded to that number of places with halves
// 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.
// 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) {
toDecimal(digits, rounded, zfill) {
if (!(digits is Num && digits.isInteger && digits >= 0)) {
if (!(digits is Num && digits.isInteger && digits >= 0)) {
Fiber.abort("Argument must be a non-negative integer")
Fiber.abort("Digits 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 qr = _n.divMod(_d)
var intPart = qr[0].toString
var intPart = qr[0].toString
Line 1,558: Line 1,561:
}
}
if (digits < 1) decPart = ""
if (digits < 1) decPart = ""
if (decPart == "") return intPart
if (decPart == "") return intPart + (zfill ? "." + ("0" * digits) : "")
return intPart + "." + decPart
return intPart + "." + decPart + (zfill ? ("0" * (digits - decPart.count)) : "")
}
}


// Convenience versions of the above which use default values for one or both parameters.
// Convenience versions of the above which use default values for some or all parameters.
toDecimal(digits) { toDecimal(digits, true) } // always rounded
toDecimal(digits, rounded) { toDecimal(digits, rounded, false) } // never trailing zeros
toDecimal { toDecimal(14, true) } // 14 digits, always rounded
toDecimal(digits) { toDecimal(digits, true, false) } // always rounded, never trailing 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.
// Returns a string represenation of this instance in the form "i_n/d" where 'i' is an integer.