How to round a number to the third decimal place?

To answer your question: no, as far as I’m aware there isn’t a function for it. That said, this is a pretty fast workaround if you only need the code a few times:

*create var 3.05
*set var * 10
*set var round(var)
*set var / 10
value of var: ${var}

Output: “value of var: 3.1”


I also coded a *gosub routine for rounding to the first decimal digit — give me a sec and I can make one that works for any amount of decimal digits, if that’s something you would be interested in.

The subroutine:

*label round_dec_1
*params varname
*set {varname} * 10
*set {varname} round({varname})
*set {varname} / 10
*return

The main code:

*create var 3.05
*gosub round_dec_1 "var"
value of var: ${var}

Same output as above.


Edit: More general subroutine as promised!

*label round_dec
*params varname digits
*temp ten_power 1
*temp times 0
*label multiplication
*set ten_power * 10
*set times + 1
*if (times < digits)
    *goto multiplication
*set {varname} * ten_power
*set {varname} round({varname})
*set {varname} / ten_power
*return

And the main code:

*create var 3.01234
*gosub round_dec "var" 3
value of var: ${var}

This rounds var to 3.012.


Edit 2: Thanks to @cup_half_empty, who has kindly informed me of the existence of the power operator (^), I’ve edited the general subroutine a bit — it looks like this now:

*label round_dec
*params varname digits
*temp ten_power (10 ^ digits)
*set {varname} * ten_power
*set {varname} round({varname})
*set {varname} / ten_power
*return

Works exactly the same as the above subroutine, just with less lines!

9 Likes