Number of digits after the comma

greenspun.com : LUSENET : WebdesignII : One Thread

Hi,

How do I limit the number of digits after the comma in tcl???? My average is geting too big!!!

Greetings, Nada

-- Nada Alzubeidi (nada@it-c.dk), November 09, 2000

Answers

Hi,

you can use the following procedure trunc2. The procedure expects a number as argument and returns the same number with only two decimals. Bug: the procedure does not handle the case 23.23a right as it returns 23.23 instead of returning an error. However, this is not important in this assignment.

proc trunc2 {tal} { if {[regsub {^([0-9]+\.?[0-9]?[0-9]?).*$} $tal {\1} res] == 0} { return "$tal is not a number" } else { return $res } }

-- Niels Hallenberg (nh@it-c.dk), November 09, 2000.


Alternatively, you can use the format command as in
  %> format "%.0f" 4343.2323
  4343
  %> format "%.0f" 43
  43
to convert a floating point value to an integer.

-- Martin Elsman (mael@it.edu), November 09, 2000.

Moderation questions? read the FAQ