This guide explains how to display numeric values with exactly two decimal places in SQL Server. This is a common requirement when presenting financial or other precise data.
The CONVERT
function provides the solution. Its syntax is:
<code class="language-sql">CONVERT(DECIMAL(precision, scale), numeric_expression)</code>
Where:
precision
: The total number of digits (both before and after the decimal point).scale
: The number of digits after the decimal point. Set this to 2
for two decimal places.numeric_expression
: The number you want to format.For instance, to format 2.999999
to show two decimal places:
<code class="language-sql">SELECT CONVERT(DECIMAL(10, 2), 2.999999);</code>
This will return 3.00
. Note that SQL Server rounds the number.
Important: The DECIMAL
data type is crucial for this task. Unlike FLOAT
or REAL
, DECIMAL
offers fixed precision and scale, ensuring accurate representation of decimal values. Choosing the appropriate precision
value depends on the expected range of your numbers.
The above is the detailed content of How to Format Numbers with Two Decimal Places in SQL Server?. For more information, please follow other related articles on the PHP Chinese website!