Decimal
Decimal data type is defined by ‘decimal(m,n)’, where ‘m’ is number of all digits and ‘n’ is the number of decimal places. Decimal is EVL custom data type.
decimal(m,n)
when ‘n’ is missing, zero is supposed
size: 8 Bytes for ‘m’ up to 18 digits
size: 16 Bytes for ‘m’ from 19 to 38 digits
Next to standard EVD options (i.e. ‘sep=’, ‘null=’, ‘quote=’, ‘optional_quote=’) decimal and thousands separator can be specified:
- decimal_sep="."
to specify a decimal separator, which can be any single ascii character below 128; by default it is a decimal point
- thousands_sep=""
defines how to separate thousands, it can be any single ascii character below 128; by default there is no thousands separator.
An EVD file example:
revenues decimal(9,4) decimal_sep="," thousands_sep="." // e.g. 12.345,6789 expenses decimal(18) // e.g. 123456789012345678 taxes decimal(18,6) thousands_sep=" " // e.g. 123 456 789 012.345678 latitude decimal(10,6) // e.g. 49.8197203 longitude decimal(10,6) decimal_sep="," // e.g. 18,1673552
Declaration in mapping
Object creation:
decimal d(); // 0 no decimal places decimal d(821); // 821 initialization from int, no dec. places decimal d(821, 3); // 821.000 initialization from int, 3 dec. places decimal d(821.658, 3); // 821.658 init. from float/double, 3 dec. places decimal d2(d, 2); // 821.65 initialization from existing object, // just change decimal places to 2 (cut off)
Manipulation, comparison
Increment/decrement:
d++; ++d; d--; --d;
All following operations can be done between two decimals or between decimal and any integral data type:
d += 100; // 921.65 d -= decimal(0.66, 3); // 920.990 d *= -2 // -1841.980 d /= 2 // -920.990 decimal d2 = d + 120; // -800.990
When adding, subtracting or dividing, the result has higher decimal places from both operands. When multiplying two decimals, the decimal places are added.
1.23 + 6.0000 = 7.2300 1.23 - 6.0000 = -4.7700 1.23 * 6.0000 = 7.380000 1.23 * 6.0000 = 0.2050
Comparison as usual: ‘==’, ‘!=’, ‘<’, ‘>’, ‘<=’, ‘>=’.
if (d > 128) ... if (d == decimal(123.456, 3)) ... if (d <= d2) ...
The decimal places can be obtained and set by the following tho methods.
d.scale() // 3 d.set_scale(2) // set the scale to 2
These methods convert decimal to other data types
int i = d.to_int(); // cut off fractional part float f1 = d.to_float(); double f2 = d.to_double(); string str = d.to_string('.', ''); // decimal_separator = '.' // thousand_separator = '' string str = d.to_string(); // use default separators
|
The reason is performance, to rounding the number, use ‘round()’ method:
decimal costs(856.128, 3); // 856.128 decimal costs_rounded = costs.round(2); // 856.130 decimal costs_cut_off(costs, 2); // 856.12