Click here to Skip to main content
15,887,998 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Is there any built-in function in sql server to format a number.

e.g. formatnumber(3.1,3) --> 3.100
formatnumber(2.314,2) --> 2.31
and so on.

i have done my own function, but just asking if i waisted my time to write it

here is my function

SQL
CREATE FUNCTION [dbo].[UDF_FORMAT_NUMBER]
(
@NUMBER    FLOAT,
@DECIMAL_PLACES INT
)
RETURNS VARCHAR(500)
AS
BEGIN

    DECLARE @NUMBER_INT_PART BIGINT
    DECLARE @NUMBER_DECIMAL_PART FLOAT
    DECLARE @DECIMAL_NUMBER_STR VARCHAR(300)
    DECLARE @NUMBER_DECIMAL_PART_STR VARCHAR(300)

    -- GET INTEGER PART ONLY
    SET @NUMBER_INT_PART = CONVERT(BIGINT,@NUMBER)

    -- THIS WILL GIVE THE NUMBER AS FLOAT ALWAYS WILL BE 0.<DECIMAL_NUMBER>
    SET @NUMBER_DECIMAL_PART = @NUMBER - @NUMBER_INT_PART

    -- NOW WE WILL GET THE DECIMAL_PART ALONE
    SET @NUMBER_DECIMAL_PART_STR = RIGHT(CONVERT(VARCHAR,@NUMBER_DECIMAL_PART),LEN(CONVERT(VARCHAR,@NUMBER_DECIMAL_PART))-2)

    -- adjust the zero padding
    SET @DECIMAL_NUMBER_STR =left(@NUMBER_DECIMAL_PART_STR + REPLICATE('0', @DECIMAL_PLACES),@DECIMAL_PLACES)

    -- final result
   RETURN CONVERT(VARCHAR,@NUMBER_INT_PART) + '.' + @DECIMAL_NUMBER_STR

END
Posted

It has been done by others:

http://www.siccolo.com/Articles/SQLScripts/how-to-create-sql-to-format-currency.html[^]

Or try something like this:
SQL
CONVERT(varchar(10), CONVERT(money, @NUMBER))


http://articles.techrepublic.com.com/5100-10878_11-5803705.html[^]

Good luck!
 
Share this answer
 
Comments
hussain.attiya 5-Aug-10 3:54am    
thanks .. i have seen the function.. but it is not what i asked about.
it is only formating a currecny number.

what i asked about is to format a number by passing it and another parameter (decimal places) count
hussain.attiya 5-Aug-10 3:56am    
Reason for my vote of 3
not what i have asked about
E.F. Nijboer 5-Aug-10 4:26am    
Well, I tried my best :) but I did found somewhat strange to round currency that way. But luckely you found the solution you needed.
any way i have the function is working perfectly.. and i have modified it.. because the above one will give error if you passed integer number.

here is the new one

SQL
CREATE FUNCTION [dbo].[UDF_FORMAT_NUMBER]
(
@NUMBER    FLOAT,
@DECIMAL_PLACES INT
)
RETURNS VARCHAR(500)
AS
BEGIN

    DECLARE @NUMBER_INT_PART BIGINT
    DECLARE @NUMBER_DECIMAL_PART FLOAT
    DECLARE @DECIMAL_NUMBER_STR VARCHAR(300)
    DECLARE @NUMBER_DECIMAL_PART_STR VARCHAR(300)

    -- GET INTEGER PART ONLY
    SET @NUMBER_INT_PART = CONVERT(BIGINT,@NUMBER)

    -- THIS WILL GIVE THE NUMBER AS FLOAT ALWAYS WILL BE 0.<DECIMAL_NUMBER>
    SET @NUMBER_DECIMAL_PART = @NUMBER - @NUMBER_INT_PART

    if @NUMBER_DECIMAL_PART>0
    begin
    -- NOW WE WILL GET THE DECIMAL_PART ALONE
        SET @NUMBER_DECIMAL_PART_STR = RIGHT(CONVERT(VARCHAR,@NUMBER_DECIMAL_PART),LEN(CONVERT(VARCHAR,@NUMBER_DECIMAL_PART))-2)
            -- adjust the zero padding
        SET @DECIMAL_NUMBER_STR =left(@NUMBER_DECIMAL_PART_STR + REPLICATE('0', @DECIMAL_PLACES),@DECIMAL_PLACES)
    end
    else
        SET @DECIMAL_NUMBER_STR = REPLICATE('0', @DECIMAL_PLACES)



    -- final result
   RETURN CONVERT(VARCHAR,@NUMBER_INT_PART) + '.' + @DECIMAL_NUMBER_STR

END
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900