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.5In this example, value is assigned the exact decimal 42.5.
Example
let distance = 3.14from distanceIn 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 = 100let finalPrice = price * (1 - discount)
from finalPriceIn 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 | 5let message = $'The rating is {rating}.'from messageIn 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: numberlet doubledHeight = height * 2from doubledHeightIn 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 = 10let b = 20.5let `sum` = a + blet difference = a - blet product = a * blet quotient = a / b
from `sum`from differencefrom productfrom quotientIn 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 = 200let taxAmount = basePrice * taxRatelet totalPrice = basePrice + taxAmount
from taxAmountfrom totalPriceIn 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.