Finding The Number Of Digits In A Number Part 1
Do you need to find the number of digits in a number?
Here we are going to write a file named digitcountp1.tcl and then we will run it as,
tclsh digitcountp1.tcl
#digitcountp1.tcl
# This procedure counts the number of digits
# in a number.
proc dcnt {num} {
set number $num;
set digicnt 0;
if {$number != 0} {
if {$number < 0} {
set number [expr {$number * -1}];
}
while {$number != 0} {
incr digicnt;
set number [expr {$number / 10}];
}
}
return $digicnt;
}
Happy reading and thank you. Meet you soon.
Comments
Post a Comment