Skip to content

Number

The number data type in Filtrera is used to represent exact decimal values. This reference guide provides a detailed specification of the number data type, its syntax, and usage for users already familiar with the concept.

Syntax

Literal number values are expressed as exact decimals using a period as the decimal point.

Basic Syntax

let value = 42.5

In this example, value is assigned the exact decimal 42.5.

Example

let distance = 3.14
from distance

In this example, distance will be 3.14.

Percentage Values

A percentage sign can be added to the end of a number (without whitespace) to create a percentage factor. For instance, 100% is the same as 1 and 50% is the same as 0.5.

Example

let discount = 20%
let price = 100
let finalPrice = price * (1 - discount)
from finalPrice

In this example, discount is 0.2, price is 100, and finalPrice is calculated as 80.

Literal Number Types

The number type, like text and boolean, can be specified as a literal type. This allows you to create types that are restricted to specific values.

Example

param rating: 1 | 2 | 3 | 4 | 5
let message = $'The rating is {rating}.'
from message

In this example, rating can only be 1, 2, 3, 4, or 5.

Type Notation for Non-Literal Number Types

When defining parameters or variables, you can use the type notation number for non-literal number types.

Example

param height: number
let doubledHeight = height * 2
from doubledHeight

In this example, height is defined as a non-literal number type, and doubledHeight is calculated by multiplying height by 2.

Practical Usage

Example: Basic Arithmetic Operations

let a = 10
let b = 20.5
let `sum` = a + b
let difference = a - b
let product = a * b
let quotient = a / b
from `sum`
from difference
from product
from quotient

In this example, basic arithmetic operations are performed on a and b, and the results are returned as a tuple (30.5, -10.5, 205, 0.4878048780487804878048780488).

Example: Using Percentage Values

let taxRate = 15%
let basePrice = 200
let taxAmount = basePrice * taxRate
let totalPrice = basePrice + taxAmount
from taxAmount
from totalPrice

In this example, taxRate is 0.15, basePrice is 200, taxAmount is calculated as 30, and totalPrice is 230.

Summary

The number data type in Filtrera allows you to work with exact decimal values effectively. By understanding its syntax and usage, including the use of percentages, you can perform a wide range of calculations accurately. Leveraging these features enables you to create precise and reliable Filtrera programs.