Interested in how to keep the balance of users in MySQL? If in FLOAT, it is rounded and the values ​​are constantly wrong. I tried the types DOUBLE and REAL, the same. The values ​​are always the same: 1.2899999, etc.

2 answers 2

Money should be stored in types with fixed accuracy. In SQL, this is numeric

    It is only necessary to store information about the monetary indicator in whole types (for example, expressed in pennies - or even in fractions of a penny - depending on what accuracy). Especially since rounding methods may differ from mathematics by law.

    • Why, if there are types with fixed precision? - German Borisov
    • Well, if simple, at least for the sake of computation speed. About accuracy you need to go deep and write a lot. And you will transfer this data to other programs that will also be counted in whole units. Significantly less likely that at some stage, someone wants to "return" to the floating point. - dSH
    • There is a sense in keeping a penny if precision is not necessary for hundredths, thousandths and so on. In the banking system, I have so far met only a type with fixed accuracy. There are problems, but they can easily be avoided with the right approach. Especially in TC 1.2899999 .... - Alexander Semikashev