2

I have an application that stores monetary amounts. I want to store this in Cloud Spanner. Using a FLOAT64 is dangerous because of well-known problems with floating point accuracy.

Other databases offer a NUMERIC type for just this kind of use-case. What should I do in Cloud Spanner?

Maxim
  • 4,075
  • 1
  • 14
  • 23
Mike Curtiss
  • 1,838
  • 2
  • 17
  • 33

2 Answers2

3

Cloud Spanner now supports NUMERIC data type with 38 decimal digits of precision, including 9 after decimal points. Please refer to the documentation here.

X.J
  • 662
  • 3
  • 6
2

Cloud Spanner does not offer a NUMERIC type or a DECIMAL type.

One workaround is to store your quantities in an INT64 column, and store them in the finest-granularity unit with which you will ever work. For example, if you are dealing with US Dollars, you could store the quantities in cents ($0.01). So $7.13 would be presented as 713 cents in your column. For some applications, you might want even finer granularity (e.g. micro-dollars) in order to minimize rounding issues.

There are other workarounds possible. For example, you could somehow serialize your numeric values to a string and store these in a STRING or BYTES column. Naively, just storing "7.13" could work, but there are of course more complex representations possible (e.g. a JSON object like "{'dollars': 7, 'cents': 13'}").

Mike Curtiss
  • 1,838
  • 2
  • 17
  • 33