EVL

Table of Contents


Products, services and company names referenced in this document may be either trademarks or registered trademarks of their respective owners.

Copyright © 2017–2020 EVL Tool, s.r.o.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts.

Decimal

EVD definition

Decimal data type is defined by ‘decimal(x,y)’, where ‘x’ is number of all digits and ‘y’ is the number of decimal places.

costs   decimal(8,2)  // 123456.78
vat     decimal(4,1)  // 123.4

Up to 18 digits the decimal occupies 8 Bytes.

From 19 to 38 digits it occupies 16 Bytes.

By default decimal separator is a decimal point ‘.’ and no thousand separator. Other separators can be specified this way:

costs   decimal(8,2) decimal_sep="," // 123456,78
vat     decimal(8,2) decimal_sep="," thousands_sep="." // 123.456,78

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

Important: By any operation, when the precision is decreased, there is no rounding, just cut off!

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