I have a line in Shopify’s Ruby script editor that is generating the error:
bad decimal operation (0 x 4)
BOX_PERCENT = BOX_DISCOUNT / BOX_PRODUCTS
BOX_DISCOUNT = 21000
BOX_PRODUCTS = 32100
both BOX_DISCOUNT and BOX_PRODUCTS are derived from Shopify script editor objects eg:
BOX_PRODUCTS = line_item.line_price.cents
Despite the error, it generates the correct calculation:
BOX_PERCENT = 0.690625
But I’m wondering how to fix the error.
If I change the calculation to the following it works fine:
BOX_PERCENT = BOX_DISCOUNT / 32100
So there must be something wrong with the formatting of BOX_PRODUCTS
EDIT:
After inspecting BOX_PRODUCTS with BOX_PRODUCTS.inspect I can see the number is a decimal:
#Decimal:0x7fb946112140
I’ve tried to convert it into a fixnum with .to_f, .to_i, .delete(‘.’).to_i and %1 but that returns the error:
[Error] undefined method ‘to_f’ for #Decimal:0x7f260aa7c140The original line_item.line_price object before I convert it to a decimal with .cents is:
#<Money: "320$"> Class: Money
2
Answers
I'm sure this is not the correct way to handle numbers but I ended up converting the original money object to a string, gsub #<Money: " and $ and then converting that to an integer:
You would try to translate the instance of
Money
into a decimal first, like this: