1.2.9

Conversions

It is often necessary to cast (convert) a value from one type to another: for example, say you want to mulitply the

`transferred`

amount by a duration value to get a new duration value. It is then necessary to convert the tez amount and the duration value into integer values in order to multiply then.The general rule is that conversions to integer are *implicit*, and conversions back to specific types (tez, duration) are not.

One conversion worthy of note is the *not* explicit conversion from tez to nat: it requires the use of the

`mutez_to_nat `

function. For example the following converts the transferred amount to a `nat`

value:1

dorequire(transferred = 11tz, "invalid transferred amount");

2

var t : nat = mutez_to_nat(transferred); // t = 11_000_000

Copied!

The following types may be *implicitly* converted to integer:

`duration`

`date`

The resulting integer value is the number of the smallest value of the source type: namely second for

`duration`

, and utez for `tez`

.The following example illustrates this mecanism:

1

effect {

2

var d : int = 1h15m; // d is the number of seconds (4500)

3

var res = t * d * 1s; // res is a duration

4

}

Copied!

Note that:

- the conversion form
`int`

to a`duration`

(line 4) is done by multiplying`t * d`

by`1s`

- the conversion from
`rational`

to`int`

is done with operators`ceil`

and`floor`

- the conversion from
`int`

to`nat`

is done with`abs`

Last modified 4mo ago

Export as PDF

Copy link

Edit on GitHub