What is the best data type to use for money in C#?
As it is described at decimal as:
The decimal keyword indicates a 128-bit data type. Compared to
floating-point types, the decimal type has more precision and a
smaller range, which makes it appropriate for financial and monetary
calculations.
You can use a decimal as follows:
decimal myMoney = 300.5m;
What data type should I use to represent money in C#?
Use System.Decimal
:
The Decimal value type represents
decimal numbers ranging from positive
79,228,162,514,264,337,593,543,950,335
to negative
79,228,162,514,264,337,593,543,950,335.
The Decimal value type is appropriate
for financial calculations requiring
large numbers of significant integral
and fractional digits and no round-off
errors. The Decimal type does not
eliminate the need for rounding.
Rather, it minimizes errors due to
rounding.
Neither System.Single
(float
) nor System.Double
(double
) are precise enough capable of representing high-precision floating point numbers without rounding errors.
Data Type Money C#
According to Microsoft type mapping guide, SQL Server's Money
type is mapped to decimal
in C#.
Note: a proper way to make your query is to use parameters, like this:
decimal prix = (decimal)(Qte * prixvente);
decimal prix_ttc = prix * (1 + (tva/ 100M));
String requete = @"UPDATE lc
SET lc.Quantite = @qte,
lc.Prix_HT = @prix,
lc.Prix_TTC = @prix_ttc
FROM LIGNES_FACTURE as lc
JOIN MM_ARTICLE as art ON lc.ID_Article=art.ID
JOIN MM_Facture as f ON lc.ID_Facture=f.ID
WHERE art.AR_Ref = @ref
AND f.Ref = @reffacture";
SqlCommand command = new SqlCommand(requete, con);
command.Parameters.Add("@qte", qte);
command.Parameters.Add("@prix", prix);
... // Set other parameters here
command.ExecuteNonQuery();
Best practices for dealing with monetary amounts in C#
You should not use floating point numbers because of rounding errors. The decimal type should suit you.
C# which is the best data type for precision 3?
The decimal
type is the best for fixed point math.
From the C# Spec Section 4.1.7:
Contrary to the float and double data
types, decimal fractional numbers such
as 0.1 can be represented exactly in
the decimal representation. In the
float and double representations, such
numbers are often infinite fractions,
making those representations more
prone to round-off errors.
Best data type to store money values in MySQL
Since money needs an exact representation don't use data types that are only approximate like float
. You can use a fixed-point numeric data type for that like
decimal(15,2)
15
is the precision (total length of value including decimal places)2
is the number of digits after decimal point
See MySQL Numeric Types:
These types are used when it is important to preserve exact precision, for example with monetary data.
Related Topics
Writing Large Number of Records (Bulk Insert) to Access in .Net/C#
New Keyword in Method Signature
How to Read and Modify Ntfs Alternate Data Streams Using .Net
How to Decode a Url Parameter Using C#
Optimal Linq Query to Get a Random Sub Collection - Shuffle
Changing the User Agent of the Webbrowser Control
Why Can't I Have Abstract Static Methods in C#
System.Text.JSON.JSONelement Toobject Workaround
How to Check the Number of Bytes Consumed by a Structure
Searching for a Specific Jtoken by Name in a Jobject Hierarchy
Parsing Large JSON File in .Net
Headless Browser for C# (.Net)
Shouldserialize*() VS *Specified Conditional Serialization Pattern
How to Calculate Float Type Precision and Does It Make Sense
C# Deserializing a Struct After Receiving It Through Tcp
Richtextbox Syntax Highlighting in Real Time--Disabling the Repaint