我应该以正数还是负数存储退款记录?

时间:2022-09-01 12:37:05

I am asking for best practice from veteran Financial programmers.

我要求资深金融程序员的最佳实践。

eg PSUDO code:

例如PSUDO代码:

class Transaction(Model):
    order = ForeignKey()
    amount = DecimalField()
    type = 'refund' or 'purchase'

If storing refunds in negative amount, then I can simply run sum() of all transactions to get the balance, math operations become a bit native.

如果以负数存储退款,那么我可以简单地运行所有交易的sum()来获得余额,数学运算变得有点原生。

If storing refunds in positive amount, then it's more human friendly on formula like purchase - refund = balance, also I don't need to invert to display a positive amount of refund in template.

如果以正数存储退款,那么它在购买时更加人性化 - 购买 - 退款=余额,我也不需要反转以在模板中显示正数退款。

Which one should I pick to have most benefits and less gotchas?

我应该选哪一个才能获得最大的收益和更少的陷阱?

1 个解决方案

#1


One approach is to have an amount field that holds the value as positive and another field like signedAmount that holds the signed version of it. So, when you want to display or log it you use amount, when you want to include it in a calculation you use signedAmount.

一种方法是将值字段保存为正值,将另一个字段保存为signedAmount,以保存其签名版本。因此,当您想要显示或记录它时,您需要使用金额,当您想要将其包含在使用signedAmount的计算中时。

#1


One approach is to have an amount field that holds the value as positive and another field like signedAmount that holds the signed version of it. So, when you want to display or log it you use amount, when you want to include it in a calculation you use signedAmount.

一种方法是将值字段保存为正值,将另一个字段保存为signedAmount,以保存其签名版本。因此,当您想要显示或记录它时,您需要使用金额,当您想要将其包含在使用signedAmount的计算中时。