public static AmountEx from_decimal(Decimal val) { AmountEx amount = new AmountEx(); amount.integral = (Int32)val; if (val < 0.0M) { amount.integral -= 1; } decimal frac = val - amount.integral; frac *= FACTOR; //amount.fraction = (UInt64)(frac + 0.5) * multiplier; amount.fraction = (UInt64)(frac * MULTIPLIER + 0.5M); if (amount.fraction >= AMOUNT_MAX_FRACTION) { amount.fraction -= AMOUNT_MAX_FRACTION; amount.integral += 1; } return(amount); }
public static string FormatAmount(AmountEx value) { if (value == null) { return(string.Empty); } if (value.Fraction == 0) { return($"{value.Integral}.0"); } string sign = string.Empty; if (value.Integral < 0) { value.fraction = AMOUNT_MAX_FRACTION - value.Fraction; value.integral += 1; sign = "-"; } var frac = value.Fraction.ToString(); frac = frac.PadLeft(18, '0'); return(sign + $"{Math.Abs(value.Integral)}.{frac.TrimEnd('0')}"); }
public static Amount GetAmountByDouble_C(decimal val) { AmountEx amountEx = AmountEx.from_decimal(val); return(new Amount(amountEx.Integral, (long)amountEx.Fraction)); }