> ## Documentation Index
> Fetch the complete documentation index at: https://dokumentation.websale.de/llms.txt
> Use this file to discover all available pages before exploring further.

# DC- (counters and calculations)

> The calculation tags of the template: number items consecutively, calculate with integers, and determine commercial amounts including VAT.

You can calculate in the template, using four families of tags that differ in what they calculate with.

| Family | Calculates with | Typical for |
| - | - | - |
| `DC-Counter1…3` | integers, only upwards in steps of one | numbering items in a loop consecutively |
| `DC-Int1…3` | integers, all basic arithmetic operations and modulo | coloring every second row, calculating grids |
| `DC-CommercialCalc1…3` | decimal numbers with price formatting | prices, VAT, discounts |
| `DC-Add()`, `DC-Sub()`, `DC-Mul()`, `DC-Div()` | two values in a single tag | a one-off calculation in place |

> [!NOTE]
>
> There are three of each of the first three families, addressed via the digit in the name: `DC-Counter1` to `DC-Counter3`. There are no more than that, and they apply to the entire page. If you are already using three counters at the same time and need a fourth, you have to reset one of them and reuse it.

## The counter

`DC-Counter1…3` counts and outputs its current value. That is all it can do, and that is exactly why it is the right choice for the most common task: numbering what a loop iterates over.

| Tag | Effect |
| - | - |
| `DC-Counter1…3_reset` | sets the value to 0 |
| `DC-Counter1…3_inc` | increases the value by 1 |
| `DC-Counter1…3` | outputs the value |

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
~DC-Counter1_reset~

{@PR-Data}
   ~DC-Counter1_inc~
   <span class="position">~DC-Counter1~</span>
   ~PR-Name~
{/@PR-Data}
```

> [!WARNING]
>
> Reset the counter before every loop. Otherwise it continues counting from the value that an earlier loop on the same page left behind.

### Reacting to specific positions

As a range tag, `DC-Counter1…3` checks against a comma-separated list of counter values.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{@PR-Data}
   ~DC-Counter1_inc~

   {DC-Counter1(1)}
      <!-- nur das erste Produkt -->
   {/DC-Counter1(1)}

   {DC-Counter1(2,4,6)}
      <!-- das zweite, vierte und sechste Produkt -->
   {/DC-Counter1(2,4,6)}
{/@PR-Data}
```

> [!NOTE]
>
> For regular patterns, listing values is impractical because the list grows with the number of products. Use `DC-Int1…3_mod()` for that instead, see below.

### Counting two lists separately

If each list should start at 1, the reset belongs at the beginning of each list.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{CAT-AdvData($C-Links,999)}
   ~DC-Counter1_reset~
   {@PR-Data}
      ~DC-Counter1_inc~ ~PR-Name~
   {/@PR-Data}
{/CAT-AdvData($C-Links,999)}

{CAT-AdvData($CS-Links,999)}
   ~DC-Counter1_reset~
   {@PR-Data}
      ~DC-Counter1_inc~ ~PR-Name~
   {/@PR-Data}
{/CAT-AdvData($CS-Links,999)}
```

## The integer variable

`DC-Int1…3` calculates with integers. All suffixes change the stored value; it is output only by `DC-Int1…3`.

| Suffix | Effect |
| - | - |
| `_set()` | sets the initial value |
| `_add()` | adds |
| `_sub()` | subtracts |
| `_mul()` | multiplies |
| `_div()` | divides |
| `_mod()` | replaces the value with the remainder of the division |
| `_inc` | increases by 1 |

All suffixes with an argument support tag-in-tag, so the initial value can come from another tag.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{@PR-Data}
   ~DC-Counter1_inc~
   ~DC-Int1_set($DC-Counter1$)~
   ~DC-Int1_add(3)~
   Ergebnis: ~DC-Int1~
{/@PR-Data}
```

> [!WARNING]
>
> A division by 0 has no effect: the value keeps its previous state, and there is neither an error message nor an abort. Therefore, check the divisor yourself if it comes from a tag.

### Every second row

This is the classic use of `_mod()`: the remainder of the division by 2 is 0 for even positions and 1 for odd ones.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{@PR-Data}
   ~DC-Counter1_inc~
   ~DC-Int1_set($DC-Counter1$)~
   ~DC-Int1_mod(2)~

   {DC-Int1(0)}<tr class="gerade">{/DC-Int1(0)}
   {DC-Int1(1)}<tr class="ungerade">{/DC-Int1(1)}
      <td>~PR-Name~</td>
   </tr>
{/@PR-Data}
```

> [!NOTE]
>
> You create a grid in the same way: `_mod(3)` results in 0, 1, 2 and repeats, so the result 0 marks every third position and thus the start of a new row.

Materials: Modulo calculation

### Bracketing by values

Like the counter, `DC-Int1…3` as a range tag checks against a comma-separated list.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{DC-Int1(0,2,4)}
   <!-- greift bei den Werten 0, 2 und 4 -->
{/DC-Int1(0,2,4)}
```

## The commercial calculation

`DC-CommercialCalc1…3` calculates with decimal numbers and outputs the result in the price formatting set in the shop configuration `shop.config`. Internally, calculations are performed with full precision; rounding only happens on output.

| Suffix | Effect on the stored value |
| - | - |
| `_set()` | sets the initial value |
| `_add()` | adds the argument |
| `_sub()` | subtracts the argument |
| `_mul()` | multiplies by the argument |
| `_div()` | divides by the argument |
| `_percent()` | replaces the value with the specified percentage of it |
| `_VAT()` | replaces the value with the VAT it contains; the value is treated as gross |
| `_VATadd()` | adds the VAT rate; the value is treated as net |
| `_VATsub()` | deducts the VAT it contains; the value is treated as gross |

> [!WARNING]
>
> `_percent()`, `_VAT()`, `_VATadd()` and `_VATsub()` differ in one point that is easily overlooked. `_VATadd()` calculates upwards from the net value, while `_VAT()` and `_VATsub()` calculate out of a gross value. For 100.00 € and 19 percent, `_VATadd(19)` therefore results in 119.00 €, whereas `_VAT(19)` results in 15.97 € and `_VATsub(19)` in 84.03 €.

### Prices as the initial value

All single tags that output a price formatted by WEBSALE are permitted as the argument. The price formatting is automatically switched off before the value is taken over.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
~DC-CommercialCalc1_set($PR-Price$)~
~DC-CommercialCalc1_VATadd(19)~
Bruttopreis: ~DC-CommercialCalc1~
```

> [!WARNING]
>
> Write fixed values in the argument without thousands separators and with a period as the decimal separator. Do not include a currency, otherwise the result is unusable.

### A discount in two steps

Two of the three calculators can be combined: one holds the initial value, the other calculates the amount to be deducted.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
~DC-CommercialCalc1_set($PR-Price$)~
~DC-CommercialCalc2_set($PR-Price$)~
~DC-CommercialCalc2_percent(10)~
~DC-CommercialCalc1_sub($DC-CommercialCalc2$)~

Rabatt: ~DC-CommercialCalc2~
Ihr Preis: ~DC-CommercialCalc1~
```

### More decimal places

`DC-CommercialCalc1…3` rounds to two decimal places. If you need more, append `_prec` and the desired number of places.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
~DC-CommercialCalc1_set(20)~
~DC-CommercialCalc1_div(3)~

~DC-CommercialCalc1~         <!-- 6,67 -->
~DC-CommercialCalc1_prec3~   <!-- 6.667 -->
~DC-CommercialCalc1_prec9~   <!-- 6.666666667 -->
```

> [!WARNING]
>
> Output with `_prec` does not use the shop's price formatting but the technical format: no thousands separator, period as the decimal separator. For amounts that the buyer is meant to read, the tag without a suffix is therefore the right choice. `_prec` is intended for values that are processed further, for example in an interface.

> [!NOTE]
>
> 1 to 10 decimal places are permitted. A higher value makes the tag invalid.

* Materials: Commercial calculations
* Guide: Removing price formatting in specific places

## One-off calculations without intermediate storage

For a calculation that is only needed in one place, there are four tags that take both values in the argument and output the result immediately. No calculator is used up for this.

| Tag | Result |
| - | - |
| `DC-Add(a,b)` | a plus b |
| `DC-Sub(a,b)` | a minus b |
| `DC-Mul(a,b)` | a times b |
| `DC-Div(a,b)` | a divided by b |

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
~DC-Mul($PR-Price$,$BASKET-PR-Quantity$)~
~DC-Div(20,3)~          <!-- 6,67 -->
```

> [!NOTE]
>
> Both arguments support tag-in-tag, and `_prec` works the same way as with `DC-CommercialCalc1…3`. These four tags are not described in the previous documentation, but they exist in the shop.

> [!WARNING]
>
> If the second value for `DC-Div()` is 0, the tag outputs nothing. The page then simply stays empty at that spot.

## Overview of the tags

| Tag | Single tag | Range tag | Negative range tag | Loop tag |
| - | - | - | - | - |
| `DC-Counter1…3()` | yes | yes | yes | – |
| `DC-Counter1…3_inc` | yes | – | – | – |
| `DC-Counter1…3_reset` | yes | – | – | – |
| `DC-Int1…3()` | yes | yes | yes | – |
| `DC-Int1…3_set()` | yes | – | – | – |
| `DC-Int1…3_add()` | yes | – | – | – |
| `DC-Int1…3_sub()` | yes | – | – | – |
| `DC-Int1…3_mul()` | yes | – | – | – |
| `DC-Int1…3_div()` | yes | – | – | – |
| `DC-Int1…3_mod()` | yes | – | – | – |
| `DC-Int1…3_inc` | yes | – | – | – |
| `DC-CommercialCalc1…3` | yes | – | – | – |
| `DC-CommercialCalc1…3_prec1…10` | yes | – | – | – |
| `DC-CommercialCalc1…3_set()` | yes | – | – | – |
| `DC-CommercialCalc1…3_add()` | yes | – | – | – |
| `DC-CommercialCalc1…3_sub()` | yes | – | – | – |
| `DC-CommercialCalc1…3_mul()` | yes | – | – | – |
| `DC-CommercialCalc1…3_div()` | yes | – | – | – |
| `DC-CommercialCalc1…3_percent()` | yes | – | – | – |
| `DC-CommercialCalc1…3_VAT()` | yes | – | – | – |
| `DC-CommercialCalc1…3_VATadd()` | yes | – | – | – |
| `DC-CommercialCalc1…3_VATsub()` | yes | – | – | – |
| `DC-Add()` | yes | – | – | – |
| `DC-Sub()` | yes | – | – | – |
| `DC-Mul()` | yes | – | – | – |
| `DC-Div()` | yes | – | – | – |

All tags are permitted in all templates.

> [!NOTE]
>
> The suffixes of the `DC-Int` and `DC-CommercialCalc` families are appended with an underscore: `~DC-Int1_add(3)~`. The previous documentation lists them without an underscore in its table of contents, but correctly in its examples.

## Related topics

* Guide: Displaying areas depending on counter values
* Guide: Removing price formatting in specific places
* Materials: Commercial calculations
* Materials: Modulo calculation
* Reference: DC- (loops)


## Related topics

- [DC- (loops)](https://dokumentation.websale.de/en/v8s/referenz/tags/dc/schleifen.md)
- [DC- (free parameters and session parameters)](https://dokumentation.websale.de/en/v8s/referenz/tags/dc/parameter.md)
- [Counters and comparisons in the template](https://dokumentation.websale.de/en/v8s/fortgeschrittene-designtechniken/zaehler-und-vergleiche-im-template.md)
- [DC- (comparisons and other tags)](https://dokumentation.websale.de/en/v8s/referenz/tags/dc/vergleiche.md)
- [OH- (Order history)](https://dokumentation.websale.de/en/v8s/referenz/tags/oh-bestellhistorie.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.