Why am I encountering a bad decimal operation error in Script Editor?

timd
Shopify Expert
122 6 75
I have a line in 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

Replies 3 (3)

timd
Shopify Expert
122 6 75

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:0x7f260aa7c140

 

The original line_item.line_price object before I convert it to a decimal with .cents is:

Inspect: #<Money: "320$">

Class: Money

Stephen2020
Shopify Partner
19 2 6

Hm,

have you already tried the following?

BOX_PERCENT = Decimal.new(BOX_DISCOUNT) / BOX_PRODUCTS
timd
Shopify Expert
122 6 75

Decimal.new generates the error: Can't convert Money into Decimal