Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing bigdecimal output

A SQL query is outputting a bigdecimal output that I expect to be 144.50, and the actual value is coming as #<BigDecimal:7fbe367ed118,'0.1445E3',18(18)>

I tried the following to compare the two:

assert_equal BigDecimal('144.50'), actual_value

but this fails with:

--- expected
+++ actual
@@ -1 +1 @@
-#<BigDecimal:7fbe367ed938,'0.1445E3',18(18)>
+#<BigDecimal:7fbe367ed118,'0.1445E3',18(18)>

Looks like the failure is because of the actual and expected values being different objects.

Is this the right way to compare big decimal objects in Ruby?

like image 271
Prakash Murthy Avatar asked Oct 24 '25 15:10

Prakash Murthy


1 Answers

Your assertion is saying "does this object equal this other object". It appears you want to compare values.

You can do that like so:

[4] pry(main)> BigDecimal('144.50') == 144.50                                             
=> true

That would make your test look something like:

assert_equal 144.50, actual_value

where actual_value is coming from the database.

like image 89
Anthony Avatar answered Oct 26 '25 06:10

Anthony



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!