Would it be bad practice to insert monetary symbols directly into the bank?

Asked

Viewed 170 times

8

In a system I had to tamper with wage values and commissions, perform calculations on them and the like. However I went to analyze that the monetary symbols are being inserted directly in the database, being a string, and inserting the values together with a Jquery mask.

Value:    R$  20

I had to transform these values of strings for float removing the currency symbol using the command str_replace() of PHP.

$value = (float)str_replace('R$  ','', $value); 

But I am doubtful if it would be right to use this practice. It would be better to create another attribute/column in the database with a name simboloMonetario and change the value of string for double?

  • 2

    Use float for things other than scientific calculations is already a problem, actually. I don’t know the proper type of PHP, but the Decimal of C# or the BigDecimal Java does the job correctly, without losing precision. Also, the information you want to store in the bank is hardly the display string, but the value of it. If the display string is saved, I cannot, for example, sum the column values efficiently

  • 1

    Best practice would be for you to have a part field identifying the currency in which the value is expressed.

  • It certainly complicates working with strings if you are going to use these fields for calculations, don’t you agree? For example, if you want to see the input and output value of a specific year, you would have to add, imagine adding strings and symbols. Of course I can cite other problems, but that’s just to get the main idea.

1 answer

10


This question is the opposite of: CPF or CNPJ field type in VARCHAR or INT database?.

In that person wanted to put descriptive data as if they were quantity and was wrong. Now this question wants to put quantities as descriptive data, which is also wrong. It’s not a matter of good or bad practice, it’s wrong, and that’s what matters. You can do it and it might even work, but it’s wrong. Monetary values are quantities and should be stored as numbers and the correct type for it is the DECIMAL (You can use an integer if you know what you’re doing and want to take the trouble to deal with it), don’t use other types that don’t have accuracy. A text type would guarantee accuracy, but create numerous problems, for example, how to sum up these values?

Note that in PHP you may also have problems, because by default PHP works with decimal part values without accuracy (most systems in PHP and other languages running around are wrong and programmers don’t even know this because use float as if they were exact, see more in ).

If you’re recording like this I wouldn’t worry more, you must be in control of everything that happens, and clearly something bad is happening. Whenever you need these conversions, cleanups, symbol swapping is doing something wrong, these things are necessary to fix the initial error, so fix it and you won’t need these things.

  • and the use of double for monetary values, could complement your response with that? grateful!

  • @Eliseub. I edited it.

  • had given error even when converting from the database, take out the monetary value, and then turn to float, the value was 1000 and ended up being transformed to 1. This would be precision problem?

  • 1

    No, that’s programming error anyway, and I didn’t say anything about accuracy in my answer.

  • 1

    I ended up confusing myself with accuracy. Thank you very much for the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.