Forum OpenACS Q&A: Response to easy tcl question...

Collapse
Posted by Michael A. Cleverly on
Here's a proc that will do it:
proc round_to_n {x n} {
    if {$n < 0} {
        error "n must be greater than or equal to 0"
    } elseif {$n == 0} {
        return [expr {int(round($x))}]
    } else {
        return [expr {round($x * pow(10,$n))/pow(10,$n)}]
    }
}
From tclsh:

% set x 7.14123
7.14123
% round_to_n $x 0
7
% round_to_n $x 1
7.1
% round_to_n $x 2
7.14
% round_to_n $x 3
7.141