存储精确小数的最佳方法

时间:2019-06-10 04:43:51

标签: c++ decimal

我需要在c ++中存储任意长度的小数。以下是我们正在考虑的结构:

<NumberAsString(char*), DecimalAt(int32)>

例如:

1.00003940400
<"100003940400", 11>

0.0004
<"0004", 4>

10
<"10", 0>

这似乎是存储小数的好方法吗?有更好的方法吗?

另一种可能的方法类似于“货币模块”通常在javascript中的工作方式,例如,将数字表示为int“美分”-$ 102.25被存储为10225。我们将像这样存储它:

<int64, int64> # the second int64 would be the decimal places

例如,如果我们允许“ 5位小数精度”,那么我们将存储:

12.250
<12, .250E5> // or <12, 25000>

哪种方法更好?或者完全使用其他方法?

0 个答案:

没有答案