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
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 95

    Assisted Solution

    by:Lee W, MVP
    You would use CAST - see:

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

    Accepted Solution

    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

    Or something like this:

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

    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)
    LVL 68

    Assisted Solution

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

    Assisted Solution

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

    Author Comment

    Thanks to all of you

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    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.

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now