# Absolute Value Percentage Difference Question

**URL:** <https://www.ruby-forum.com/t/absolute-value-percentage-difference-question/151390>\
**Category:** Rails\
**Created:** [October 20, 2008, 10:19pm UTC](https://www.ruby-forum.com/t/absolute-value-percentage-difference-question/151390 "2008-10-20T22:19:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [October 20, 2008, 10:19pm UTC](https://www.ruby-forum.com/t/absolute-value-percentage-difference-question/151390/1 "2008-10-20T22:19:53Z")

</div>

Hi.

I’ve got a report where I’m making comparisons between 2 different  
values. It’s possible for the percentage difference from one value to  
the next to be less than or greater than the original number.

If value 2 is 20% greater than OR 20% less than value 1, I want to  
highlight that row in a table that I created.

Any thoughts on how to write the correct if statement that would yield  
this result?

So far I have diff = (value1 - value2) / value1

.5 = (10-5)/10 which is a 50% decrease between the 2 values

Since the percentage difference is greate than 20%, how can I test that  
50% in an if statement to highlight this difference for review?

THANKS!

---

<div class="post-metadata">

**Author:** ![newrubygirl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e274bd/32.png) [@newrubygirl](https://www.ruby-forum.com/u/newrubygirl)\
**Post date:** [October 20, 2008, 10:47pm UTC](https://www.ruby-forum.com/t/absolute-value-percentage-difference-question/151390/2 "2008-10-20T22:47:57Z")

</div>

On Oct 20, 2008, at 4:19 PM, Becca G. wrote:

> Any thoughts on how to write the correct if statement that would yield
> 
> THANKS!

def highlight\_difference(value1, value2, close\_enough=0.8…1.2)  
return nil if value1.nil? || value2.nil?  
return nil if value1.zero?  
return nil if close\_enough.include?(value2.to\_f/value1)  
true  
end  
irb\> highlight\_difference(10, 5)  
=\> true  
irb\> highlight\_difference(10, 8)  
=\> nil  
irb\> highlight\_difference(10, 12)  
=\> nil  
irb\> highlight\_difference(10, 15)  
=\> true

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
