Calculating percentages

  • Hi all,

     

    just noticed something strange with sql server- if you divide one number by another that is larger 0 will be returned. how do you calculate percentages that are not round numbers.

    eg (a/b)*100

     

    I've tried casting the result as a decimal but this still returns, eg 7.0000, which is no good to me

     

    help!

     

    thanks,

    Alex

  • sincere apologies have foudn the answer..

  • In case anyone's wondering >> (a/b.0) * 100

  • Remi,

    select (426/852)/100.0

    Returns .000000

    What did I do wrong?

  • i think that this is the correct select

    select (426.0/852.0)/100.0

  • 426/852.0 I guess

  • The int/int in brackets will result in int before the division by 100.0

    Try

    select (426.0/852)/100

    Far away is close at hand in the images of elsewhere.
    Anon.

  • wow we all got it right, a point to everyone, except remi of course

    Far away is close at hand in the images of elsewhere.
    Anon.

  • Actually,

    only Jesper got it right

    either   select 426.0/852

      or

    Select 426/852.0

    with or without parens will return .50

    select (426.0/852) /100.0  returns .0050

     

    Thanks all, this tip will help simplify some of my calculations!

  • Don't know what you're talking about .

  • OK, OK, so a bit of a faux pas

    answer should have been  (426.0/852)*100 based on first post instead of correcting remi

    but my explanation was right though

    if you really want to be picky the right answer to the post should be

    select (cast(a as float)/b)*100

    or

    select (a/cast(b as float))*100

    or

    select ((a+0.0)/b)*100

    or

    select (a/(b+0.0))*100

    or

    select ((a*1.0)/b)*100

    or

    select (a/(b*1.0))*100

    etc.. etc.. etc..

    take ya pick

    Far away is close at hand in the images of elsewhere.
    Anon.

  • Ok David, so is the .0  sort of a shortcut to force the float calculation?

  • int * int = int

    int * float = float

    So that's not really a shortcut, but a mathematical truth .

  • Ah, redeemed!  Thanks Remi.

    I always appreciate your answers.

  • Redeemed??? I wouldn't be the kind of person to make 2 mistakes in 3 characters, wolud Y?

    .

Viewing 15 posts - 1 through 15 (of 20 total)

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