> For the complete documentation index, see [llms.txt](https://docs.eximee.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.eximee.com/budowanie-aplikacji/logika-biznesowa/scriptcode/skrypty-scriptservice/api-skryptow/operacje-matematyczne-w-scriptcode.md).

# Operacje matematyczne w ScriptCode

## Operacje matematyczne w ScriptCode

Zalecane jest, aby **wszystkie operacje matematyczne** w `scriptCode` wykonywać poprzez obiekt `BigDecimal`.

***

### Interfejs API

#### `big-decimal.interface.ts`

**BigDecimal**

```typescript
interface BigDecimal {
  valueOf(value: string | number | BigDecimal): BigDecimal
  add(augend: string | number | BigDecimal): BigDecimal
  subtract(subtrahend: string | number | BigDecimal): BigDecimal
  multiply(multiplicand: string | number | BigDecimal): BigDecimal
  divide(divisor: string | number | BigDecimal): BigDecimal
  divide(divisor: string | number | BigDecimal, roundingMode: string): BigDecimal
  divide(divisor: string | number | BigDecimal, scale: number): BigDecimal
  divide(divisor: string | number | BigDecimal, scale: number, roundingMode: string): BigDecimal
  setScale(newScale: number): BigDecimal
  setScale(newScale: number, roudingMode: string): BigDecimal
  equals(equated: string | number | BigDecimal): boolean
  compareTo(compared: string | number | BigDecimal): int 
}
```

#### `decimal-format.interface.ts`

```typescript
interface DecimalFormat {
  of(pattern: string): DecimalFormat
  of(pattern: string, symbols: DecimalFormatSymbols): DecimalFormat
  format(value: BigDecimal): string
  parse(value: string): BigDecimal
}
```

#### `rounding-mode.interface.ts`

```typescript
enum RoundingMode {
  UP,
  DOWN,
  CEILING,
  FLOOR,
  HALF_UP,
  HALF_DOWN,
  HALF_EVEN
}
```

#### `decimal-format-symbols.interface.ts`

```typescript
enum DecimalFormatSymbols {
  PL,
  EN,
  ES,
  SK,
  CZ
}
```

#### `random.interface.ts`

```typescript
interface Random {
  uuid(): string
}
```

### BigDecimal

#### Tworzenie obiektu

```typescript
var value = BigDecimal.valueOf("10.1000"); // Zalecane
```

{% hint style="warning" %}
Obiekt `BigDecimal` jest **niezmienny (immutable)**.\
Operacje takie jak `add()` nie zmieniają wartości, wymagają przypisania wyniku do zmiennej.
{% endhint %}

***

#### Operacje matematyczne

**Dodawanie**

```typescript
let value = BigDecimal.valueOf("1");
value = value.add("5"); // 1 + 5 = 6
```

**Odejmowanie**

```typescript
let value = BigDecimal.valueOf("100");
value = value.subtract("5"); // 100 - 5 = 95
```

**Dzielenie**

```typescript
let value = BigDecimal.valueOf("100");
value = value.divide("10"); // 100 / 10 = 10
value = value.divide("3", 5, RoundingMode.UP); // 10 / 3 = 3.33334
```

**Mnożenie**

```typescript
let value = BigDecimal.valueOf("1");
value = value.multiply("20"); // 1 * 20 = 20
```

**Ustawienie skali**

```typescript
value = BigDecimal.valueOf("10.1000");
value = value.setScale(2); // = 10.10
value = value.setScale(1, RoundingMode.HALF_UP); // = 10.1
```

**Porównanie i równość**

```typescript
value.equals("10"); // false
value.compareTo("5"); // 1
```

***

### DecimalFormat

Klasa do formatowania liczb dziesiętnych w różnych językach.\
Dokumentacja: [DecimalFormat (Java)](https://docs.oracle.com/javase/8/docs/api/java/text/DecimalFormat.html)

#### Przykład

```typescript
let decimalFormatPL = DecimalFormat.of("#.#", DecimalFormatSymbols.PL);
decimalFormatPL.format(BigDecimal.valueOf("34.567")); // 34,6
```

#### Patterns

| Symbol | Znaczenie                                |
| ------ | ---------------------------------------- |
| `0`    | Zastępca dla cyfry                       |
| `#`    | Zastępca dla cyfry (0 pomijane)          |
| `,`    | Separator dziesiętny                     |
| `%`    | Mnoży przez 100 i wyświetla jako procent |
| `E`    | Notacja naukowa                          |

***

### Zaokrąglanie liczb (`RoundingMode`)

| Tryb        | Opis                                             |
| ----------- | ------------------------------------------------ |
| `HALF_UP`   | Zaokrągla w górę, jeśli część dziesiętna ≥ 0.5   |
| `CEILING`   | Zawsze w górę do najbliższej liczby całkowitej   |
| `DOWN`      | Zawsze w dół                                     |
| `FLOOR`     | W dół do najbliższej liczby całkowitej           |
| `HALF_DOWN` | W dół jeśli < 0.5                                |
| `HALF_EVEN` | Zaokrąglenie "bankierskie" (do liczby parzystej) |

***

### Preferowane skale wyników

<https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/math/BigDecimal.html>

| Operacja    | Skala wyniku                                |
| ----------- | ------------------------------------------- |
| Add         | `max(addend.scale(), augend.scale())`       |
| Subtract    | `max(minuend.scale(), subtrahend.scale())`  |
| Multiply    | `multiplier.scale() + multiplicand.scale()` |
| Divide      | `dividend.scale() - divisor.scale()`        |
| Square root | `radicand.scale()/2`                        |

***

### Random

Generowanie identyfikatorów UUID (wersja 4):

```typescript
function callService(context) {
  return [{ 'output': Random.uuid() }];
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.eximee.com/budowanie-aplikacji/logika-biznesowa/scriptcode/skrypty-scriptservice/api-skryptow/operacje-matematyczne-w-scriptcode.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
