A
A
Andrey Mensky2015-06-14 21:45:24
JavaScript
Andrey Mensky, 2015-06-14 21:45:24

Data type for money in JavaScript and nodejs?

Good day, we all know about the detrimental use of float as a data type for money manipulations. There was a need to work with finances in my application.
What libraries would you recommend for operations and data formatting?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrey Mensky, 2015-06-15
@men232

Thank you very much everyone, found the solution in this - https://github.com/davidkalosi/js-money

T
Timur Shemsedinov, 2015-06-14
@MarcusAurelius

The question is rather about the type of variables in JavaScript, but in the node with types as well. So in js there is one type for "number" digits. It stores both integers and fractions. Of course, store integers with the maximum required accuracy, for example, instead of 7.35, store 735, and if you need accuracy above kopecks, then store, for example, for a thousandth of a penny: 735000 and then divide and round, for example Math.round(735123/1000) = 735 or Math.round(735123/1000)/100 = 7.35 and cast to string (Math.round(735123/1000)/100).toString() = "7.35"

K
Konstantin Kitmanov, 2015-06-15
@k12th

You can try something for big arithmetic to make sure you don't lose precision: https://www.npmjs.com/package/bignumber.js

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question