Why is division in Ruby returning an integer instead of decimal value?
It’s doing integer division. You can make one of the numbers a Float
by adding .0
:
9.0 / 5 #=> 1.8
9 / 5.0 #=> 1.8
Why does my division operator keep returning a zero?
When calculating chances
you're doing integer division, which will return a result rounded down to the nearest integer. In this case, you're seeing 0
because your result is meant to be a percentage (since it's less than 1, it rounds down to 0 in integer division).
To return the actual value you'll want to convert one of the integers to a Float
, like so:
chances = p1Overall / (p1Overall + p2Overall).to_f
Hope this helps!
Integer division with rounding
You can do it while remaining in the integer world as follows:
def round_div(x,y)
(x + y / 2) / y
end
If you prefer, you could monkey-patch Fixnum
with a variant of this:
class Fixnum
def round_div(divisor)
(self + divisor / 2) / divisor
end
end
187.round_div(100) # => 2
Why a simple calculation returns different result in ruby
That's because of the data type ruby uses for dividing, int
is missing the fractional part of the result.
In Ruby :
289 / 30
=> 9
9 * 30
=> 270
289.0 / 30
=> 9.633333333333333
In Python (for example):
>>> 289 / 30
9.633333333333333
>>> 9.63333 * 30
288.9999
Why does ruby round like this?
This has nothing to do with rounding.
Ruby does division differently on float than it does on an integer.
If you divide integers, you will always get an integer result.
If you divide with floats (or a mixture of integer and float), you will always get a float result.
Remember your order of operations, too. Ruby is going to handle the division before it handles the addition.
7/100 = 0
so 1+0 = 1
7.0/100 = 0.07
so 1+0.07 = 1.07
Ruby Divide Always Returning Zero in Rails Axlsx Export
Since both numbers are integers, ruby is doing integer division, which ignores remaining fractions, and I would guess all your divisions are returning numbers between 0 and 1, so the answer is always 0, to avoid that one of the both numbers should be a float so like @mtm suggested, add a to_f
function to either of the numbers
Why in Ruby 0.0/0, 3.0/0 and 3/0 behave differently?
In Ruby, not all numbers are created equal (pun intended).
Decimal numbers (0.0
, 3.0
) follow the IEEE 754-2008 standard for floating point arithmetic:
The standard defines
arithmetic formats: sets of binary and decimal floating-point data, which consist of finite numbers (including signed zeros and subnormal numbers), infinities, and special "not a number" values (NaNs)
Whole numbers (0
, 3
) are treated as integers.
Both NaN
and Infinity
(as well as -Infinity
) are special cases that such floats are designed to handle, but integers are not -- hence the error.
Rails Not Dividing Variables Correctly
It results in zero because you have to provide floats. This should work:
@total_course_lessons = @course_lesson_ids.count.to_f
@total_completed_lessons = @completed_lessons.count.to_f
percent = @total_completed_lessons / @total_course_lessons * 100
Related Topics
Ruby Class Instance Variable Vs. Class Variable
Ruby: String Comparison Issues
Oo Design in Rails: Where to Put Stuff
Nokogiri Installation Fails -Libxml2 Is Missing
How to Force Rails_Env in a Rake Task
Get List of a Class' Instance Methods
How to Pass Arguments into a Rake Task With Environment in Rails
Class ≪≪ Self Idiom in Ruby
Installing Bootstrap 3 on Rails App
What Does a Double * (Splat) Operator Do
Rails: What's a Good Way to Validate Links (Urls)