Database decimal is not returning the complete number


#1

I’ve a database column as decimal(8,2). Then i’ve a database entry with 750.00 for example.
But when i query the database, i’m just getting 750 back, not 750.00 If i store a value like 70.67 i’m getting the exact result.

If i store 10.50 i am getting 10.5 back and not 10.50.
Can someone give me a hint, what i’m missing here?


#2

Okay i solved it with a cool npm pacakge called nummeraljs

And in my model i’ve a getter

getInvoiceNet(amount) {
    return numeral(amount).format('$ 0,0.00');
  }

#3

Yeah that’s normal behavior of a float, the DB will always store the number of positions before and after the decimal point that you specify in the field, but javascript doesn’t show extra zeros after the decimal point.

The numeral package formats numbers as text. You could also accomplish it with .toFixed(2) to enforce 2 decimal places.

BTW: Monetary values are usually better stored as an integer (in cents, rather than dollars for example) then their value divided by 100 when prepping for display. This avoids rounding errors and such when calculating taxes and so forth. A lot of payment providers (stripe, for example) take the value to be charged as an integer as well. :wink: