Scid  4.7.0
utils.tcl
Go to the documentation of this file.
1 
2 
3 # thousands, percentFormat:
4 # Functions to format integer numbers.
5 # thousands inserts the thousands separator (usually "," or ".") for
6 # every three digits before the decimal separator in the number.
7 # percentFormat does the same as thousands, but also adds a percentage.
8 # If "kilo" is true, numbers >= 100,000 are divided by 1000 and have
9 # the unit "K" appended while values over 1 million appear as "1.00M"
10 #
11 proc ::utils::thousands {n {kilo 0}} {
12  global locale
13  set commaChar [string index $locale(numeric) 1]
14  set unit ""
15  if {$kilo} {
16  if {$n >= 1000000} {
17  set decimalChar [string index $locale(numeric) 0]
18  set decimalPart [format "%02d" [expr {(int($n / 10000)) % 100}]]
19  set n [expr {int($n) / 1000000}]
20  set unit "${decimalChar}${decimalPart}M"
21  } elseif {$n >= 100000} {
22  set unit "K"
23  set n [expr {int($n / 1000)}]
24  }
25  }
26  if {$commaChar == ""} { return "$n$unit"}
27  while {[regsub {^([-+]?[0-9]+)([0-9][0-9][0-9])} $n "\\1$commaChar\\2" n]} {}
28  return "$n$unit"
29 }
30 
31 proc ::utils::percentFormat {num denom} {
32  # Ensure denominator is not zero:
33  if {$denom == 0} {set denom 1}
34  return "[::utils::thousands $num] ([expr $num * 100 / $denom]%)"
35 }
36