Why are people using Strings to represent numbers in DynamoDB?

I have watched and read a lot of tutorials about DynamoDB in the past week. One common pattern I see, is that people use strings to represent numbers, e.g. a price on a product?

Why is this the case? Does it has to do with the floating point representation?

maybe because of this issue

https://github.com/boto/boto3/issues/369

that some did a string for numbers

For a price, decimal is the way to go though.

That sounds like an issue with floating point precision on ids which is one of the many reasons I always make sure the id/partition key is a string.

Numbers in DynamoDB are strings:

Numbers are sent across the network to DynamoDB as strings, to maximize compatibility across languages and libraries. However, DynamoDB treats them as number type attributes for mathematical operations.

If these tutorials are using the N attribute type, they’re doing what they should do with numbers.

Ehhh, pretty sure repainting a price in cents (aka just a number) is a lot closer to the industry standard. Or am I misunderstanding what you are saying?

Representing a number in cents can lead to errors if you apply a division (such as when applying rebates in %).
Using Decimal can help you keep the correct number, and then choose wether to floor or ceil it to the nearest cent.

Note that the error might be small, but small errors can add up quickly and become expensive when talking about money.

That’s fair, for our discounts we add a discount object that specifies the amount (in cents) off, so we have already rounded to the nearest cent before we store the discount but I see your point.

The trick is to skim all those rounding errors off into another account.

Just don’t put the decimal in the wrong place, or you’d better watch your cornhole good buddy

I think using decimal is much more error-prone than using integer cents. It forces the programer to account for those rounding error when and where they appear, such as you said, division (and really not much other operations). Using decimal compounds these errors, and hides them into a singular rounding. When you are doing financial operations, you must be able to explain who gets the extra cent when splitting 10$ 3 ways. “We round it up at the end of whatever math we do” is not sufficient for accounting purposes. You also need those 3 amounts to add back up to 10$, simple rounding will not guarantee that.

Another (general) reason, apart from the ones mentioned already, may be, that frontend/backend expect some field to hold either strings or numbers and are too lazy to cast them to the appropriate type before saving.
Sometimes this may even be advantageous.

Lots of prices have fractional cents. For example, AWS server costs per hour.

Laziness and/or incompetence.

It’s pretty common in legacy apps that deal with money to store dollar amounts in thousandths of a dollar (1000 = $1 or 0025 = 2.5 cents) or larger if the app has to deal with fractions of a penny.

That seems like a pretty solid answer

In addition to the stuff already discussed there is one thing that hasn’t been touched upon, key overloading. When building a single table design on DynamoDb it’s common to use same keys to represent different data in different types/rows. Eg, the key/column could just use a general name like sortKey and depending on the item/row type it could hold a string or number.

Javascript handling of numbers in json is a nightmare. Python is not much better.

Just losing precision seems more expensive then reformatting a number in code or taking up a little bit more storage.

Could you suggest some DynamoDB tutorials? I am a beginner and I have been having a tough time selecting a dynamodb tutorial.

I actually started with the dynamodb book https://www.dynamodbbook.com/