proc format_usd {num {sep ,}} { # Find the whole number and decimal (if any) set whole [expr int($num)] set decimal [expr $num - int($num)] #Basically convert decimal to a string set decimal [format %0.2f $decimal] # If number happens to be a negative, shift over the range positions # when we pick up the decimal string part we want to keep if { $decimal <=0 } { set decimal [string range $decimal 2 4] } else { set decimal [string range $decimal 1 3] } # If $decimal is zero, then assign the default value of .00 # and glue the formatted $decimal to the whole number ($whole) if { $decimal == 0} { set num $whole.00 } else { set num $whole$decimal } # Take given number and insert commas every 3 positions while {[regsub {^([-+]?\d+)(\d\d\d)} $num "\\1$sep\\2" num]} {} # Were done; give the result back return $num }