Skip to content

rounded_convert

Maarten Hilferink edited this page Sep 27, 2026 · 7 revisions

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.

Since GeoDMS 20.22.0 a conversion to an unsigned integer type rounds in that type. Before, it rounded in the signed type of the same size and stopped at the maximum of that signed type, so every value above it came out wrong: rounded_convert(200.2, uint8) gave 127, 40000.4 into uint16 gave 32767 and 3e9 into uint32 gave 2147483647, and points with unsigned coordinates likewise per coordinate. Results for such values calculated with an older version have to be recalculated.

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);

Clone this wiki locally