How to model currency values in Strapi?

Viewed 554

I am setting up my first Strapi project.

I am still a bit curious, how you are able to model all the typical business models.

For example if I need to store prices/amounts in a database I would go for an integer field in the database.

$1.40 are represented by 140 (cents). That keeps things like MySQL's DECIMAL type and float/double issues away.

Is it possible to use some sort of viewtransformers to input Decimal Values like "1.40" and let the real value be 140 in the strapi content ui?

1 Answers

I suggest in your case to only work with cents.

And manage the display on your frontend application.

If you want to manage that through the API endpoint you will have to customize your find and findOne function in your controller.

Here is a guide that shows you how to customize data response - https://strapi.io/documentation/3.0.0-beta.x/guides/custom-data-response.html

Instead of removing data, you will have to update your price value.

Related