Solved

# 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
512 Views
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).
0
Question by:wsfindlater

LVL 95

Assisted Solution

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

LVL 14

Accepted Solution

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

LVL 14

Assisted Solution

Or something like this:

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

0

LVL 16

Assisted Solution

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

LVL 68

Assisted Solution

AFAIK, STR() function is the closest you can get now in SQL Server.
0

LVL 16

Assisted Solution

So something like: LTRIM(STR(Value, 20, 2))
0

Author Comment

Thanks to all of you
0

## Featured Post

### Suggested Solutions

This is my first ever Article on EE or elsewhere; therefore, please bear with me if I have some discrepancies in my writing. I read many articles and questions related to "how to pass values to SSIS packages at run-time?"  Hence, this common ques…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.