1. Home
  2. Docs
  3. Personalization
  4. Reference
  5. Math Functions

Math Functions

Integer vs floating point

FeedBlitz’s math functions are relatively intuitive. For example <$eval 1+2+3+4$> returns 10. It also follows operator precedence rules, such that <$eval 3+3/3$> returns 4, not 2. You can override (or clarify) mathematical order of operations with parentheses, so while <$eval 3+(3/3)$> is still 4, <$eval (3+3)/3$> becomes 2. So far, so basic.

Unless numbers are made to be floating point, FeedBlitz uses integer arithmetic. This may yield unexpected results when dividing, as remainders are dropped. For example, <$eval 20/3$> returns 6, not 7. So if you do this <$eval 20/3*3$>, which common sense says is exactly 20, FeedBlitz (like any other integer only system) will return 18, because 20/3 is 6 in integer math, so multiplying that by 3 gives 18. A workaround for integer math is to multiply before dividing, or rewriting <$eval 20/3*3$> as <$eval 20*3/3$> which gives 20*3, or 60, dividing by 3, gets us back to the expected 20.

This is particularly important for percentages. Let’s say we have 50 items and we sell 10, what percentage has been sold? <$eval 10/50*100$> gives 0, because in integer math, 10/50 is 0. Multiplying first gives <$eval 100*10/50$> or 20, which is the expected percentage.

To avoid integer math, we can switch to floating point. If any number or string has a decimal point, the templating engine uses floating point math. (If you’re not sure whether floating point is in use, just force it by multiplying the first value by 1.0).

FeedBlitz’s floating math runs to 6 decimal places, which sounds good, but has its issues for the unwary, because sometimes floating point math can be very precise (6 decimal places!) but a teeny bit inaccurate, and that inaccuracy is where you need to be careful.

Consider <$eval 20/3*3$> again, but now make it floating point: <$eval 20.0/3*3$> – but we will start with just the first part, <$eval 20/3$>. This evaluates to 6.666667 (it’s really the fraction 6 2/3, but in decimals that’s 6 point 6 recurring, and because FeedBlitz only has 6 decimal places, it correctly rounds that last decimal place up to a 7). So <$eval 20.0/3*3$> ends up being 20.000001 thanks to that final 7 in the last decimal place – not far off 20, but not exactly 20 either. Again, multiplying first fixes this, but the trap for the unwary is where you test <$eval 20==20.0/3*3$> and the answer comes back 0 (because 20.000001 is not the same as 20; it’s a rounding error).

The solution when using comparison operators after doing math, then is:

  • Multiply first
  • If using floating point, use functions like money() to eliminate rounding errors.

And with that discussion done, here are the math functions.

In all of the examples, numeric constants are used. However these will typically be custom fields in a real templating implementation e.g. <$if money(payment)>=1000.00 $>Big Sale!<$endif$>. In addition, in a template, these functions would need to be inside an <$if$> or <$eval$> tag. These tags have been omitted from this page for clarity.

abs

Returns the absolute value of the number, N. In other words, if N is positive, it returns N. If N is negative, it returns -1.0 * N.

Usage

  • abs(N)
  • N abs

Examples

  • abs(-12) – returns 12
  • -11.2 abs – returns 11.2

floor

Returns the first whole number (integer) less than equal to N. For negative numbers, this might be counter-intuitive.

Usage

  • floor(N)
  • N floor

Examples

  • floor 12.1 – returns 12
  • floor 12.6 – returns 12
  • floor 12.0 – returns 12
  • floor(-12.6) – returns -13
  • floor(-12.1) – returns -13
  • floor(-12.0) – returns -12

ceil

Returns the first whole number (integer) greater than or to N. For negative numbers, this might be counter-intuitive.

Usage

  • ceil(N)
  • N ceil

Examples

  • ceil 12.1 – returns 13
  • ceil 12.6 – returns 13
  • ceil 12.0 – returns 12
  • ceil(-12.6) – returns -12
  • ceil(-12.1) – returns -12
  • ceil(-12.0) – returns -12

round

Rounds to the nearest whole number (integer).

Usage

  • round(N)
  • N round

Examples

  • round 12.1 – returns 12
  • round 12.6 – returns 13
  • round 12.0 – returns 12
  • round(-12.6) – returns -13
  • round(-12.1) – returns -12
  • round(-12.0) – returns -12

min

Returns the smallest of two numbers, X and Y. May be counterintuitive for negative numbers.

Usage

  • min(X,Y)
  • X min Y

Examples

  • min(12.1,11) – returns 11
  • min(12.6,-11) – returns -11
  • min(12.0,-12.5) – returns -12.5
  • min(-12.6,-11) – returns -12.6
  • min(-12.1,11) – returns -12.1
  • min(-12.0,-12.5) – returns -12.5

max

Returns the largest of two numbers, X and Y. May be counterintuitive for negative numbers.

Usage

  • max(X,Y)
  • X max Y

Examples

  • max(12.1,11) – returns 12.1
  • max(12.6,-11) – returns 12.6
  • max(12.0,-12.5) – returns 12.0
  • max(-12.6,-11) – returns -11
  • max(-12.1,11) – returns 11
  • max(-12.0,-12.5) – returns -12.0

money

Rounds a number N to two decimal places. Useful both for displaying financial information, and eliminating the effects of rounding errors in floating point math.

Usage

  • money(N)
  • N money

Examples

  • money(20) – returns 20.00
  • money(20.000001) – returns 20.00
  • 20.0/3*3 money – returns 20.00
  • 24.491 money – returns 24.49
  • 24.495 money – returns 24.50
  • 24.994 money – returns 24.99
  • 24.996001 money – returns 25.00

commaformat

Displays the amount with a comma thousands separator.

Usage

  • commaformat(N)
  • N commaformat

Examples

  • commaformat(20) – returns 20
  • commaformat(2001) – returns 2,001
  • commaformat(20.95) – returns 20.95
  • 2001234 commaformat – returns 2,001,234
  • 2001234.90 commaformat – returns 2,001,234.90