gpt4 book ai didi

currency - 存储货币时的常识?

转载 作者:行者123 更新时间:2023-12-01 01:25:36 29 4
gpt4 key购买 nike

在阅读了如何做到最好之后 handle users in multiple timezones正确地,我了解到要走的路是将所有日期存储在规范化的、应用程序范围的时区中 - UTC然后在输出时应用标准化时区和单个用户时区之间的差异。今天我开始考虑将这种方法应用于软件中的货币处理是否合适:

所有存储的货币都转换为应用程序范围的货币,比如 EUR (€),并且在输出时,货币会转换回用户自己的货币,并具有当天更新的汇率?

这里有什么常识?这通常是如何解决的,在选择处理此问题的方法之前我应该​​注意什么?

最佳答案

一种标准方法是在持有和操纵货币值(value)时同时存储金额和货币。

Money Pattern在 Martin Fowler 的企业应用程序架构模式中。

Fowler 描述了定义一个简单的数据类型来保存两个原始组件,并使用重载的算术运算符来执行货币操作:

"The basic idea is to have a Money class with fields for the numeric amount and the currency. You can store the amount as either an integral type or a fixed decimal type. The decimal type is easier for some manipulations, the integral for others. You should absolutely avoid any kind of floating point type, as that will introduce the kind of rounding problems that Money is intended to avoid. Most of the time people want monetary values rounded to the smallest complete unit, such as cents in the dollar. However, there are times when fractional units are needed. It’s important to make it clear what kind of money you’re working with, especially in an application that uses both kinds. It makes sense to have different types for the two cases as they behave quite differently under arithmetic.

Money needs arithmetic operations so that you can use money objects as easily as you use numbers. But arithmetic operations for money have some important differences to money operations in numbers. Most obviously, any addition or subtraction needs to be currency aware so you can react if you try to add together monies of different currencies. The simplest, and most common, response is to treat the adding together of disparate currencies as an error. In some more sophisticated situations you can use Ward Cunningham’s idea of a money bag. This is an object that contains monies of multiple currencies together in one object. This object can then participate in calculations just like any money object. It can also be valued into a currency."

关于currency - 存储货币时的常识?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7574745/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com