8
I found here in the SO a question with an answer until well accepted by the community What kind of data (double, float or decimal) should I use to represent currency in . NET with C#?
I just never had a problem with double
for monetary calculations, I did have float
this one already left me standing hair, so from this reply stating that Decimal is correct, or this is just p/ who uses C# and nothing to do with Mysql, I started testing it, and left me a little insecure, for example say that I will always store 2 floating point digits, there are cases where the user will "need" (exception) record the value with 4 digits in the bank that in turn will record only the first two, for this reason I feel safer with double
.
Question - there is an example source code that shows me this "error" rounding of Double for me to see to believe, or a reference source book, trusted website that signals this recommendation of Decimal use to record monetary values on Mysql?
Obs 1 - The data types I refer to are from Mysql as I also saw a quote from MSDN https://msdn.microsoft.com/pt-br/library/364x0z75.aspx except that it is C# reference and forgive me for MS supporters I need a more "secure" source when I say trusted site ( because I’m talking about Mysql, not SQL Server, . Net, C# etc.)
Obs 2 - Has a link from mysql itself http://dev.mysql.com/doc/refman/5.0/en/fixed-point-types.html my English is not very good, see if I am mistaken, says:
Maximum number of digits for DECIMAL is 65
Maximum number of digits for DECIMAL Is 65
And in the sequence it says
Before Mysql 5.0.3, the Maximum range of DECIMAL values is the same as for DOUBLE
In other words, the biggest DECIMAL is equal to DOUBLE.
Continuing the text of the site
but the actual range for a Given DECIMAL column can be constrained by the Precision or Scale for a Given column
That part I understand is DECIMAL which is limited by precision or I am wrong ?
I will put here a test, I hope someone shows me this "mistake" in the same way (similarly)
CREATE TABLE `teste` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`numero` double NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
INSERT INTO `test`.`teste` (`numero`) VALUES ('0.1');
INSERT INTO `test`.`teste` (`numero`) VALUES ('99.9');
SELECT sum(numero) FROM teste;
mysql> SELECT sum(numero) FROM teste;
+-------------+
| sum(numero) |
+-------------+
| 100 |
+-------------+
1 row in set (0.00 sec)
I was in doubt because I found a link where a "moderator" says that Mysql 5.x deprecated Decimal http://www.scriptbrasil.com.br/forum/topic/76609-doubledecimal-ou-float/
– SneepS NinjA
Look at the float error, very clearly. And double has the same problem: http://sqlfiddle.com/#! 9/6d5af/2
– bfavaretto