Posted By

Phoenicia7 on 05/14/12


Tagged


Versions (?)

Round


 / Published in: TCL
 

  1. proc _round {num index} {
  2. # Procedure to round numerical results to any decimal or whole number.
  3. # The decimal or whole number digit is indicated using an index.
  4. # Index 0 is the nearest integer.
  5. # Decimals are indicated by positive indicies: 1 or the first decimal, 2 for the second, etc.
  6. # Whole numbers are indicated by negative indicies: -1 for nearest 10th, -2 for the nearest hundreth, etc.
  7. # Example:
  8. #
  9. # Number: 9 9 9 . 9 9 9
  10. # Index: -2 -1 0 1 2 3
  11. #
  12. # Index 1 = 0.x
  13. # Index 2 = 0.0x
  14. # Index 3 = 0.00x
  15. # Index 0 = x
  16. # Index -1 = x0
  17. # Index -2 = x00
  18. # Index -3 = x000
  19.  
  20. set multiplier [expr pow(10,$index)]
  21. return [expr (round(($num * $multiplier)) / $multiplier)]
  22. }
  23.  

Report this snippet  

You need to login to post a comment.