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

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).
wsfindlaterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lee W, MVPTechnology and Business Process AdvisorCommented:
You would use CAST - see:

(one example that's does things slightly differently than you ask)
http://www.codeproject.com/Purgatory/Formatting_in_SQL.asp
0
Muhammad Ahmad ImranDatabase DeveloperCommented:
I think you are looking for this:

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

Syntax
ROUND ( numeric_expression , length [ , function ] )

Arguments
numeric_expression

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

length

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.

function

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.

Remarks
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


Examples
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)
GO

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)
 123.4500
 
SELECT ROUND(123.45, -2)
 100.00
 


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)
 151.00
 
SELECT ROUND(150.75, 0, 1)
 150.00
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Muhammad Ahmad ImranDatabase DeveloperCommented:
Or something like this:

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

LeoAhmad
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

muzzy2003Commented:
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)
0
Scott PletcherSenior DBACommented:
AFAIK, STR() function is the closest you can get now in SQL Server.
0
muzzy2003Commented:
So something like: LTRIM(STR(Value, 20, 2))
0
wsfindlaterAuthor Commented:
Thanks to all of you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.