Absolute difference between two numbers

Hello,

I started writing a “need help” post but I found it out how to do writing :slight_smile:

I needed to check that the difference between two prices is higher than 0.01.
PriceA - PriceB > 0.01 gives a wrong result if PriceA < PriceB.

As there is no “:absolute” function, square can be used : PriceA - PriceB ^2 ^0.5
But (5.00 - 5.01)^2 ^0.5 gives something like 0.010001, which is > 0.01, because of the rounding in the calculation

so use a higher limit such as (PriceA - PriceB)^2 ^0.5 > 0.0101

5 Likes

I haven’t tried it myself yet, but check out the Mathjs plugin and use the abs() function.

1 Like

Haven’t thought about MathJS yet, thanks @Scott !

Thanks @nicolas.daprigny for this great square / square root trick to get absolute value!

It’s almost six years later but your advice is still valid – Bubble still doesn’t have an absolute value operator (or even parentheses – sheesh!)

Calculator plugins like MathJS are good, but they run client-side and therefore can’t be used for backend workflow calculations. Or they rely on API calls to MathJS, which has daily limits.

Your trick is the only way I could find to get absolute value in the backend without API. Lifesaver!

You re welcome. You bring me back… More than 5 years ago !!

1 Like

Works like a charm. Thanks for this.