10-21-2018, 04:13 +0200
Search result (1)
All posts by: robeadam
AuthorPost
Topic: [TCL] Round a number (5.55 => 5.6 instead of 5.5 as 'format %.2f' would do it)   Forum: #tclcode snippets      Goto post
robeadam  10-28-2011, 00:29
Member since 10/2011
1 Post
Location: Johns Creek, GA
This was exactly what I was looking for but after playing with it a bit, I'd recommend 1 change.  If the user passes the proc 1 digit, the returned value would be \d.\d which is great.  But if the user doesn't pass digits or passes it as 0, the return will be \d.0 which is inaccurate.  So the enhancement would be:

proc round {number {digits 0}} {

   if { $digits} {
      return [expr {round(pow(10,$digits)*$number)/pow(10,$digits)}]
   } else {
      return [ int [expr {round(pow(10,$digits)*$number)/pow(10,$digits)}]]
   }
}

Thanks for the snippet, helped a lot.
Go to forum
Unclassified NewsBoard 1.5.3-d | © 2003-4 by Yves Goergen | Time: 206.2 msec, CPU time: 66 msec, 28 Database queries in 161.7 msec | 2.8 kB (9.7 kB) | Timezone: +0200