rounded_convert

Conversion functions rounded_convert

definition

The rounded_convert function is similar to the Convert function, except that the value is rounded to the nearest integer, instead of taking off the decimals.

A conversion to an unsigned integer type rounds in that type, over its whole range: rounded_convert(200.2, uint8) is 200, 40000.4 into uint16 is 40000 and 3e9 into uint32 is 3000000000. Points with unsigned coordinates are rounded in the same way, per coordinate.

In the following example, the 2.60[euro] in float32 representation is not exactly 2.60, as there is no exact binary floating point representation for this number. Instead, its binary representation is m*2^-e with m and e integer, which is more like 2.599999…, and the amount in (non-rounded) cents is (after multiplying by 100) 259.9999…, which, when converted to an integer representation with convert is rounded towards zero and becomes 259. When rounding to the nearest integer is more appropriate, use the rounded_convert function.

unit<float32> Euro      := BaseUnit('euro', float32);
unit<uint16>  Ct        := uint16(0.01f * euro);

parameter<euro> euros           := 2.60[euro];
parameter<Ct>   centen          := convert(euros, Ct);
parameter<Ct>   centen_rounded  := rounded_convert(euros, Ct);