How to remove trailing zeros and decimal?

  • Hi, this is my first post here. I tried searching for this and didn't have luck, so I apologize if this has been asked before (perhaps I'm searching the wrong terms as I'm a noob).

    I'm trying to edit an SSRS report in Visual Studio '08. It's basically a list of opportunities grouped in columns by sales stage. Below the list are a few fields that sum the estimated revenue subtotals and totals. The value expressions are pretty basic in that it just pulls the opportunity name and appends the revenue in parenthesis and adds the letter M for million. So from the db it'll pull "ABC Company" and "1500000" and show "ABC Company (1.50M)".

    I would like to trim the trailing zeros and/or the decimal point when needed. For example, 1.30 should be 1.3, 6.00 should be 6, 0.70 should be 0.7, and 0.00 just 0 by itself.

    Below are the expressions in one column I'm working with and I was hoping somone could explain what code to insert and how it fits into my existing expressions. Thanks in advance!

    =Fields!name.Value + " (" + FormatNumber(CStr(CDbl(Fields!new_revenue_baseValue.Value)/1000000),2) + "M) " + IIf(Parameters!Ownership.Value = "True"," [" + Fields!ownerid.Value + "]", "")

    =(Sum(Fields!stage1.Value, "New")) / 1000000

    =(Sum(Fields!stage1.Value, "Existing")) / 1000000

    =(Sum(Fields!stage1.Value,"Existing") + Sum(Fields!stage1.Value,"New"))/1000000

  • I don't even know how to spell "SSRS" but, if I were to do something like this in T-SQL, here's one way to do it IF there were always going to be just 2 decimal places. Hopefully, you can connvert it to the language of SSRS.

    --===== Create some test data. This is NOT a part of the solution.

    DECLARE @SomeTable TABLE (SomeDecimal DECIMAL(9,2));

    INSERT INTO @SomeTable (SomeDecimal)

    SELECT 1.50 UNION ALL

    SELECT 6.00 UNION ALL

    SELECT 0.70 UNION ALL

    SELECT 0.03 UNION ALL

    SELECT 100.01 UNION ALL

    SELECT 0.00

    ;

    --===== This could be turned into an Inline Table Valued Function for ease of use.

    WITH

    cteSplit AS

    (

    SELECT IntPart = CAST(CAST(SomeDecimal AS INT) AS VARCHAR(30)),

    DecPart = RIGHT(SomeDecimal % 1, 2)

    FROM @SomeTable

    )

    SELECT IntPart

    + ISNULL('.' + NULLIF((LEFT(DecPart,1) + ISNULL(NULLIF(RIGHT(DecPart,1),'0'),'')),'0'),'')

    FROM cteSplit

    ;

    As a bit of a sidebar, this is an unusual report display requirement. Why do they want it this way?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Thanks for your reply. They want it this way to save space. Right now, it rounds off to 2 decimal places--so why have "Opportunity (2.00M)" when you can have "Opportunity (2M)". The three saved characters help keep the text on one line since the columns are side by side. Hence, by the time all opportunities are listed, the hope is that they fit on one page when it's exported as a PDF, word, etc.

    Is there a way to accomplish this through a value expression in VB.net? I'm not familiar with TSQL, nor would I know where to start entering it. :/

  • jchicky (11/20/2012)


    Thanks for your reply. They want it this way to save space. Right now, it rounds off to 2 decimal places--so why have "Opportunity (2.00M)" when you can have "Opportunity (2M)". The three saved characters help keep the text on one line since the columns are side by side. Hence, by the time all opportunities are listed, the hope is that they fit on one page when it's exported as a PDF, word, etc.

    Is there a way to accomplish this through a value expression in VB.net? I'm not familiar with TSQL, nor would I know where to start entering it. :/

    I'm sure there is but I'm not a VB guy. Hopefully this post will server as a "bump" to entice one to take a shot at it. 🙂

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Here is what I used to populate my DataSet

    SELECT 'ABC' AS Company, 1000000 AS Revenue UNION ALL

    SELECT 'DEF' AS Company, 1550000 AS Revenue UNION ALL

    SELECT 'GHI' AS Company, 2100000 AS Revenue UNION ALL

    SELECT 'JKL' AS Company, 2010000 AS Revenue

    Here is the Expression I Used

    =Fields!Company.Value + " (" + CStr(SUM(Fields!Revenue.Value)/1000000) + "M)"

    and It Does exactly what your asking.

    By Using the Function FormatNumber(<<Expr>>,2) Your telling it to force the Number to be 2 Decimal places.

    If you CStr a Number it automatically removes the trailing zeros.

    if you don't trust it, a more explicit removal is in order I suppose.

    But this looks way worse.

    ="(" + iif(CStr(FormatNumber(CStr(CDbl(Fields!Revenue.Value)/1000000),2)).EndsWith("00"),Left(CStr(FormatNumber(CStr(CDbl(Fields!Revenue.Value)/1000000),2)),Len(FormatNumber(CStr(CDbl(Fields!Revenue.Value)/1000000),2))-3),iif(CStr(FormatNumber(CStr(CDbl(Fields!Revenue.Value)/1000000),2)).EndsWith("0"),Left(CStr(FormatNumber(CStr(CDbl(Fields!Revenue.Value)/1000000),2)),Len(FormatNumber(CStr(CDbl(Fields!Revenue.Value)/1000000),2))-1),CStr(FormatNumber(CStr(CDbl(Fields!Revenue.Value)/1000000),2)))) + "M)"

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply