Division returns zero
You are working with integers here. Try using decimals for all the numbers in your calculation.
decimal share = (18m / 58m) * 100m;
Division of integers returns 0
You should cast before you divide, but also you were missing a subquery to get the total count from the table. Here's the sample.
select
random_int,
count(random_int) as Count,
cast(count(random_int) as decimal(7,2)) / cast((select count(random_int) from test) as decimal(7,2)) as Percent
from test
group by random_int
order by random_int;
Division of two values returning 0 rather than positive or negative decimal
Guess you are working on SQL Server. Then change RETURN_ON_SALES to Decimal(10,10) and CAST the inputs or change its data type to DECIMAL
DECLARE @RETURN_ON_SALES decimal(10,10);
DECLARE @TW_UNIT_SALES INT ;
DECLARE @DISTRIBUTION INT ;
SET @TW_UNIT_SALES=7;
SET @DISTRIBUTION=41;
SET @RETURN_ON_SALES=CAST(@TW_UNIT_SALES AS DECIMAL (10,5))/ CAST (@DISTRIBUTION AS DECIMAL(10,5));
PRINT @RETURN_ON_SALES;
Integer division always zero
You are doing integer division.
Try the following and it will work as expected:
int x = 17;
double result = 1.0 / x;
The type of the 1
in the expression you have above is int
, and the type of x
is int. When you do int / int
, you get an int back. You need at least one of the types involved to be floating point (float
or double
) in order for floating point division to occur.
Unlike in Mathematics, division in C++ can either refer to truncated integer division (what you did) or floating point division (what I did in my example). Be careful of this!
In my example, explicitly what we have is double / int -> double
.
Division in double variable returning always zero
The result of 80/100
(both integers) is always 0.
Change it to 80.0/100.0
Division result is always zero
because in this expression
t = (1/100) * d;
1 and 100 are integer values, integer division truncates, so this It's the same as this
t = (0) * d;
you need make that a float constant like this
t = (1.0/100.0) * d;
you may also want to do the same with this
k = n / 3.0;
Division by 0.9 c# always returns 0
My guess is that the type of m_LocationSqrMtr
is int
, in which case this expression:
700 / m_LocationSqrMtr
... will be computed using integer arithmetic, and the result converted to float
. I suspect you want:
if (m_LocationCosts > 450 && m_LocationCosts < 700)
{
m_DefaultPricing = 700f / m_LocationSqrMtr;
}
The f
suffix on the literal means that it's a float
literal, so first m_LocationSqrMtr
will be promoted to float
, and then the division performed using float
arithmetic.
However, if this is meant to be representing currency values, you should consider using decimal
instead of float
- and then probably rounding the value to 2 decimal places. If you do all your currency arithmetic in decimal
, you're less likely to run into unexpected results...
Why does Python return 0 for simple division calculation?
In Python 2, 25/100
is zero when performing an integer divison. since the result is less than 1
.
You can "fix" this by adding from __future__ import division
to your script. This will always perform a float division when using the /
operator and use //
for integer division.
Another option would be making at least one of the operands a float, e.g. 25.0/100
.
In Python 3, 25/100
is always 0.25
.
Related Topics
SQL to Entity Framework Count Group-By
How to Connect to SQL Server from Another Computer
Call a Stored Procedure with Another in Oracle
Get Next Sequence Value from Database Using Hibernate
Perform This Hours of Operation Query in Postgresql
SQL "Select Where Not in Subquery" Returns No Results
Fastest Check If Row Exists in Postgresql
Easiest Way to Populate a Temp Table with Dates Between and Including 2 Date Parameters
How to Declare Global Variable in SQL Server..
How to Deal with Concurrent Updates in Databases
Insert Update Stored Proc on SQL Server
How to Delete from Multiple Tables in the Same SQL Statement
Why Is Using '*' to Build a View Bad
Alternatives to Limit and Offset for Paging in Oracle