[Last Call] Learn how to a build a cloud-first strategyRegister Now


How does one in SQL format a floating point number to be output with a fixed number of decimal places?

Posted on 2004-11-17
Medium Priority
Last Modified: 2012-05-05
How does one in SQL format a floating point number to be output with a fixed number of decimal places?
In Visual Basic it would be Format(x,"#0.00"), where x is a floting point number.
In C it would be printf("%4.2f ",x).
Question by:wsfindlater
LVL 97

Assisted Solution

by:Lee W, MVP
Lee W, MVP earned 200 total points
ID: 12610650
You would use CAST - see:

(one example that's does things slightly differently than you ask)
LVL 14

Accepted Solution

Muhammad Ahmad Imran earned 1000 total points
ID: 12610651
I think you are looking for this:

Returns a numeric expression, rounded to the specified length or precision.

ROUND ( numeric_expression , length [ , function ] )


Is an expression of the exact numeric or approximate numeric data type category, except for the bit data type.


Is the precision to which numeric_expression is to be rounded. length must be tinyint, smallint, or int. When length is a positive number, numeric_expression is rounded to the number of decimal places specified by length. When length is a negative number, numeric_expression is rounded on the left side of the decimal point, as specified by length.


Is the type of operation to perform. function must be tinyint, smallint, or int. When function is omitted or has a value of 0 (default), numeric_expression is rounded. When a value other than 0 is specified, numeric_expression is truncated.

Return Types
Returns the same type as numeric_expression.

ROUND always returns a value. If length is negative and larger than the number of digits before the decimal point, ROUND returns 0.

Example Result
ROUND(748.58, -4) 0

ROUND returns a rounded numeric_expression, regardless of data type, when length is a negative number.

Examples Result
ROUND(748.58, -1) 750.00
ROUND(748.58, -2) 700.00
ROUND(748.58, -3) 1000.00

A. Use ROUND and estimates
This example shows two expressions illustrating that with the ROUND function the last digit is always an estimate.

SELECT ROUND(123.9994, 3), ROUND(123.9995, 3)

Here is the result set:

----------- -----------
123.9990    124.0000    

B. Use ROUND and rounding approximations
This example shows rounding and approximations.

Statement Result
SELECT ROUND(123.4545, 2)
SELECT ROUND(123.45, -2)

C. Use ROUND to truncate
This example uses two SELECT statements to demonstrate the difference between rounding and truncation. The first statement rounds the result. The second statement truncates the result.

Statement Result
SELECT ROUND(150.75, 0)
SELECT ROUND(150.75, 0, 1)
LVL 14

Assisted Solution

by:Muhammad Ahmad Imran
Muhammad Ahmad Imran earned 1000 total points
ID: 12610658
Or something like this:

select cast( 10.550000000000001 as decimal(5,2))


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 16

Assisted Solution

muzzy2003 earned 600 total points
ID: 12612441
Not particularly keen on this but it does the job:

CONVERT(varchar(10), CAST(Value AS money), 0)

If your data type is actually money or smallmoney, then this becomes much more workable:

CONVERT(varchar(10), Value, 0)
LVL 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 200 total points
ID: 12615119
AFAIK, STR() function is the closest you can get now in SQL Server.
LVL 16

Assisted Solution

muzzy2003 earned 600 total points
ID: 12615352
So something like: LTRIM(STR(Value, 20, 2))

Author Comment

ID: 12616032
Thanks to all of you

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question