Solved

Rounding numbers in ASP

Posted on 2004-10-31
268 Views
Last Modified: 2006-11-17
I have a number .075 which is being pulled from a sql server database.  I want to mulitply the number by 100 and display it as a percentage.  My code below always rounds the value up to 8.00% where as I would rather it say 7.50%.  How can I fix this.

formatnumber(ccur(rsRec("discount"))*100,2)%>%

Thanks,
Jon
0
Question by:kerrj
    9 Comments
     
    LVL 48

    Accepted Solution

    by:
    This works fine for me:

    <%=formatnumber(cdbl(".075")*100,2)%>

    then problem may be is on the value of rsRec("discount")

    try check it and make sure its value is .075 ?
    0
     
    LVL 48

    Expert Comment

    by:Ryan Chong
    oops. <%=formatnumber(ccur(".075")*100,2)%> also works for me, returned 7.50% as expected
    0
     
    LVL 46

    Expert Comment

    by:fritz_the_blank
    I don't see why you even need the CCur() method here. Why not just:

    formatnumber(rsRec("discount")*100,2)%>%

    or

    round(rsRec("discount")*100,2)%>%

    FtB
    0
     
    LVL 7

    Expert Comment

    by:Garve
    Is the field in the database and integer field (long or short)? If so the number will be being rounded up when entered into the db. Try setting the number type as 'double'.
    0
     
    LVL 46

    Expert Comment

    by:fritz_the_blank
    I am not sure how the datatype could be integer if it is storing a value of .075

    FtB
    0
     

    Author Comment

    by:kerrj
    The reason why I have to do a conversion on the data is because it is a numeric data type in SQL Server.  I get a Type mismatch if you do not do a conversion.  The option of coverting to Cdbl rounds to 8.00 as well.  There must be a way to do this other than changing the data type?

    Thanks,
    Jon
    0
     

    Author Comment

    by:kerrj
    Yes the conversion will work if you hard code the numbers in, it will not work if you pull the data from SQL Server database.
    0
     
    LVL 46

    Expert Comment

    by:fritz_the_blank
    If it is numeric, then you should be able to use the round function. I think that there is something that I am missing here.

    FtB
    0
     

    Author Comment

    by:kerrj
    round(rsRec("discount")*100,2)  'returns a type mismatch error
    round(rsRec("discount"),2)*100  'returns the value as 8% when it should be 7.5% (the DB value is .075)

     ryancys was correct originally.  During the time I was testing this someone actually changed the value to .08 so I changed it back to .075 again to test and it does work with Cdbl() as mentioned above or as follows...

    Cdbl(rsRec("discount"))*100

    Thanks,
    Jon
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    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

    I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
    I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
    This video Micro Tutorial is the first in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles al…
    This video discusses moving either the default database or any database to a new volume.

    846 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

    10 Experts available now in Live!

    Get 1:1 Help Now