# $Header: /home/cvsroot/tcldb/ucodb/Tlib/chkNum,v 1.2 1999/03/09 22:43:03 de Exp $
# tcl procs saved on Sun Sep 03 16:18:54 PDT 1995

proc chkNum {which num} {



# This is from thw wisql 1.4 code (DC)
#       which is I or F
#       num is a string pretending to be a number
#       we will return a 1 (bad, unacceptable) or a 0
#       plus the fixed number... plus an optional message.
#       if you put in -$67.90.763 you should get out
#                       1 -67.90763



	if {[string trimleft $num] == ""} {
		return "0 NULL"
	}

#       more than one word?  take only the first
        if {[llength $num] > 1} {
                set num [lindex $num 0]
        }

#       save the original input
        set onum $num

#       First get rid of all $ and commas
        set neg 0
        set fixed 0
        set money 0

        set res [regsub -all \\\$ $num "" num]
        if {$res} {
        }

        set res [regsub -all , $num "" num]
        if {$res} {
        }

#       We have to keep sign though so check if there is a -

        if {[crange $num 0 0] == "-"} {
                set neg 1
        }
#
#	Which regsub syntax?

	set tclv [lindex [split [info tclversion] .] 0]
#	Pre-v7 version
	if {$tclv < 7} {

        set res [regsub -all - $num "" num]

	} else {

        set res [regsub -all -- - $num "" num]

	}

        if {$res} {
		set fixed 1
        } 

#       Now strip periods and check for numericity (!)

        set res [regsub -all \\\. $num "" temp]

        if {$res} {
        } else {
		set temp $num
	}

        if {[ctype digit $temp] == 0} {
                return {1 NOT}
        }

        if {[string first . $num] == -1} {
                if {$which == "F"} {
                        set num [format "%.2f" $num]
                }
        } else {
                case $which in {
                {F} {
                        set ind [string first . $num]
#		if it starts with a . then fake it up to start with a 0

			if {$ind == 0} {
				set temp "0$temp"
				set ind 1
			}

                        if {$money} {
                        set fnum [format "%s.%s"  [crange $temp 0 [expr {$ind - 1}]]  [crange $temp $ind [expr {$ind + 1}]]]
                        } else {
                        set fnum [format "%s.%s"  [crange $temp 0 [expr {$ind - 1}]]  [crange $temp $ind end] ]
                        }

                        if {$fnum != $num} {
                        set fixed 1
                        }
                        set num $fnum
                }
                {I} {
                        set ind [string first . $num]
                        set num [crange $num 0 [expr {$ind - 1}] ]
                        set fixed 1
                }
                }
        }


        if {$neg} {set num [format "-%s" $num]}

        if {$fixed} {
        set msg "I had to change $onum to $num to make it a valid number type $which."
        } else {
        set msg ""
        }

#       return list of 3 items.
        set retval "0 $num"
        lappend retval $msg
        return "$retval"
}

