Search code examples
google-cloud-datastore

What is the best way to store a Java BigDecimal using the Google Cloud Datastore API


I need to save financial data to cloud datastore (not through appengine, using the cloud datastore API beta); The price information is store in our object using BigDecimal.

According the api docs, only double and long are supported as numeric types.

I am concerned about saving the prices using BigDecimal's doubleValue() method due to the nature of double and precision, the other way would be using toString and saving as a String but this feels inefficient.

What do you suggest being the appropriate approach ?

Thanks


Solution

  • I would recommend saving it as a String value (especially if it is financial data). Simply because then you can restore the value with perfectly predictable result by means of BigDecimal(String) constructor.

    See BigDecimal(double) constructor notes in its javadoc.