In Apex, the Decimal data type is used to represent fixed-point decimal numbers with high precision. A Decimal is a 32-bit data type that provides accurate representation of decimal values.
public class DecimalExample {
public static void main() {
Decimal price = 99.99;
Decimal taxRate = 0.0825;
System.debug('Price: ' + price);
System.debug('Tax Rate: ' + taxRate);
// Performing arithmetic operations with Decimal values
Decimal totalPrice = price * (1 + taxRate);
System.debug('Total Price (including tax): ' + totalPrice);
}
}
We declare a Decimal variable price and initialize it with a price value.
We declare a Decimal variable taxRate and set it to a tax rate of 8.25%.
We use System.debug() to print the values of price and taxRate.
We perform an arithmetic operation to calculate the total price, including tax, and store it in the totalPrice variable.