Get digits of Shipping rate Money object or convert to integer

davekeql
Tourist
7 0 6

I would like to check the amount of a shipping rate and see what it ends with, so I can then discount a set amount.

 

Basically I want to be able to do modulo math.  Like 

 

if shipping_rate.price.cents % 100 == 95
  # discount specific amount

 

But I can't find any way to get a range of digits from a Money object in Shopify Scripts.  And I don't see how to convert a Money object to an integer.

 

My use case:  we have a bunch of shipping rates for differing amounts, some from apps using carrier calculated rates, some from Shopify shipping profiles.  We want to discount only the amount of the Shopify provided rates, not the app provided rates.

 

The problem is when rates are merged ("source: Shopify").  I can detect when merged, but I can't discount just the app provided rates.

 

There's a bunch of different instances where we may or may not want to apply a discount, primarily cart thresholds.  I can figure it out using some simple modulo math due to how are rates are set up — but I can't figure out how to access those final two digits within a Shopify Script.


How do I convert a Money object to an integer or compare to just certain digits in a money object?

Replies 3 (3)

davekeql
Tourist
7 0 6

Solved with few conversions.  `cents` is a Decimal.  I converted `to_s` and then `to_i`.

 

Hacky, but looks like this:  

last_two = shipping_rate.price.cents.to_s.to_i % 100

strandead10
Visitor
3 0 0

Thanks for the information!

strandead10
Visitor
3 0 0

If you're looking to calculate shipping rates and apply discounts based on the final digits, modulo math is the way to go! It allows you to determine the remainder after dividing a number, which can be useful for your purpose. By using the modulo operator, you can easily extract the last digits of the shipping rate, enabling you to apply targeted discounts accordingly. To streamline the process, consider exploring online tools or programming languages that support modulo operations. For a comprehensive understanding and practical implementation, resources like https://casinoplinko.com could provide valuable insights into utilizing modulo math effectively for your shipping rate calculations and discount strategies. Happy shipping and discounting!