September 21, 2005 at 11:13 am
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
September 21, 2005 at 11:18 am
sincere apologies have foudn the answer..
September 21, 2005 at 11:31 am
In case anyone's wondering >> (a/b.0) * 100
September 22, 2005 at 7:43 am
Remi,
select (426/852)/100.0
Returns .000000
What did I do wrong?
September 22, 2005 at 7:50 am
i think that this is the correct select
select (426.0/852.0)/100.0
September 22, 2005 at 7:51 am
426/852.0 I guess ![]()
September 22, 2005 at 7:52 am
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.
September 22, 2005 at 7:54 am
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.
September 22, 2005 at 8:09 am
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! ![]()
September 22, 2005 at 8:23 am
Don't know what you're talking about ![]()
.
September 22, 2005 at 8:23 am
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.
September 22, 2005 at 8:41 am
Ok David, so is the .0 sort of a shortcut to force the float calculation?
September 22, 2005 at 8:46 am
int * int = int
int * float = float
So that's not really a shortcut, but a mathematical truth
.
September 22, 2005 at 8:50 am
Ah, redeemed! Thanks Remi.
I always appreciate your answers.
September 22, 2005 at 8:54 am
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 21 total)
You must be logged in to reply to this topic. Login to reply