Sql 2000 floats without displaying decimal when doing division

I do a percentage calculation, but it displays integers. I tried converting from int to float, but it still displays as integer. How to fix it to display decimal numbers.

The calculation I'm using is 100.0 * (1.0 + (floatA / (floatA + floatB)))

this is where floatA = 1, floatB = 1, so I should see a value of 50.0%, instead I see a value of 100.

I am trying to get things to use decimals. Any help would be appreciated.

+4
source share
3 answers

If floatA and floatB are the values ​​returned by COUNT , then they will be integers.

You need to point out at least one of the operands involved in the division to float / decimal to avoid integer division in this part of the expression.

 100.0 * (1.0 + (floatA / (floatA + CAST(floatB as float)))) 
+5
source

If you use floatA = 1 and floatB = 1, then arithmetic with pencil and paper should give you 150. But these are integers, not floats.

But on most SQL platforms, this statement

 select 1 / (1 + 1) 

will return zero.

Division of integers .

it

 select 100.0 * (1.0 + (1.0/ (1.0 + 1.0))) 

should give you 150.

+1
source

Just casting one of your values ​​into a float type solves the problem.

 select (12345/6789) 

the above example will return 1, i.e. the result will be truncated to an integer

 select (cast(12345 as float)/ cast(6789 as float)) select (12354/ cast(6789 as float)) 

these examples will return 1.81838267786125

+1
source

Source: https://habr.com/ru/post/1347186/


All Articles