The documentation of Decimal.js
states the following:
precision
The maximum number of significant digits of the result of an
operation.
All functions which return a Decimal will round the return value to
precision significant digits except Decimal, absoluteValue, ceil,
floor, negated, round, toDecimalPlaces, toNearest and truncated.
Well, if you really need that behavior globally, then simply implement a wrapper type that does that, you've got a good specification to go by:
public struct RoundedDecimal
{
public static int Precision { get; private set; }
public static Decimal AbsoluteValue(
RoundedDecimal d) => Math.Abs(d.value);
//same with ceiling, floor, etc.
private readonly decimal value;
public RoundedDecimal(decimal d)
{
value = Decimal.Round(d, Precision);
}
public static void SetPrecision(int precision)
{
Precision = precision; /*omitted argument validation*/ }
public static implicit operator Decimal(
RoundedDecimal d) => d.value;
public static explicit operator RoundedDecimal(
decimal d) => new RoundedDecimal(d);
public static RoundedDecimal operator +(
RoundedDecimal left, RoundedDecimal right) =>
new RoundedDecimal(left.value + right.value);
//etc.
}
Performance wise this will not be very impressive but if its the behavior you need then, by all means, implement it!
DISCLAIMER: written code on my cell phone, so its bound to have bugs... simpy trying to get the idea across.