Formatting percent and dealing with division by zero problems

Discussion in 'ASP General' started by Bill, Aug 29, 2003.

  1. Bill

    Bill Guest

    I have some values that I want to display as percent, such as the
    retail price/wholesale price. In some instances, the wholesale price
    is zero, so I get a division by zero error.

    What can I do to avoid this?

    Also, how can I get this to only show two decimals, instead of it
    going .##### the way it does. I want it to look like .45%

    Thanks a million,

    Bill
    Bill, Aug 29, 2003
    #1
    1. Advertising

  2. > I have some values that I want to display as percent, such as the
    > retail price/wholesale price. In some instances, the wholesale price
    > is zero, so I get a division by zero error.
    >
    > What can I do to avoid this?


    if wholesaleprice > 0 then
    response.write retailprice / wholesaleprice
    else
    response.write retailprice / retailprice
    end if

    > Also, how can I get this to only show two decimals, instead of it
    > going .##### the way it does. I want it to look like .45%


    Look at the formatnumber / formatpercent functions.
    Aaron Bertrand [MVP], Aug 29, 2003
    #2
    1. Advertising

  3. Bill

    dlbjr Guest

    function Divide(strNom,strDenom,intDecimal)
    Divide = 0
    if IsNumeric(strNom) and IsNumeric(strDenom) then
    if CDbl(strNom) > 0 and CDbl(strDenom) > 0 then
    if IsNumeric(intDecimal) then
    intDecimal = FormatNumber(CDbl(Abs(intDecimal)),0)
    else
    intDecimal = 2
    end if
    Divide = FormatNumber(CDbl(strNom) / CDbl(strDenom),intDecimal)
    end if
    end if
    end function

    'Example
    Response.Write Divide(234,321,3)




    -dlbjr

    invariable unerring alien
    dlbjr, Aug 29, 2003
    #3
  4. Bill

    Bill Guest

    Thank you for the function! For your own knowlede, in order to calculate
    the percent markdown from a retail price, the correct way to do it is
    (denominator - numerator)/denominator

    so if we're selling a $10 dollar item for $9, and we want to show that
    is a 10% discount, it would be
    (10-9)/10
    1/10 = 10%

    Your equation simply showed the numerator over the denominator.

    By the way, this isn't adding the percent sign, I have to concatenate it
    manually. Any way for that formatting to happen with a function?

    Thanks again,

    Bill

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Bill, Aug 29, 2003
    #4
  5. > By the way, this isn't adding the percent sign, I have to concatenate it
    > manually. Any way for that formatting to happen with a function?


    Have you looked at the formatpercent function?
    Aaron Bertrand - MVP, Aug 29, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Razvan
    Replies:
    77
    Views:
    13,744
    Joseph Daniel Zukiger
    Oct 19, 2004
  2. Christopher Benson-Manica

    Doubles and zero/negative zero

    Christopher Benson-Manica, Jun 30, 2004, in forum: C Programming
    Replies:
    4
    Views:
    656
    Walter
    Jul 1, 2004
  3. Replies:
    94
    Views:
    4,378
    ┬Ča\\/b
    Feb 9, 2007
  4. sandeep

    Division by zero

    sandeep, Jan 15, 2010, in forum: C Programming
    Replies:
    18
    Views:
    806
    Paul N
    Jan 18, 2010
  5. Replies:
    2
    Views:
    104
    kiana
    May 11, 2006
Loading...

Share This Page