› Clovertech Forums › Read Only Archives › Cloverleaf › Cloverleaf › padding string with zeros
I often do something like this for padding:
if {[lindex $xlateInVals 0] != “”} {set xlateOutVals [list [format “%05s” [lindex $xlateInVals 0]]]}
It’s just an example that you’d need to modify for your own needs.
-- Max Drown (Infor)
Let me add one more thing. The inbound string could be any vary of numbers up to 10 digits. So it may be 123, 1234, 123456 etc. The output will need to be padded with zeros, so the string is 10 digits total.
Never mind I figured it out. Thx Max for showing me the way… 😆
Gary,
You can use Max’s code to do this by simply changing the “%05s” to
“%010s”. This will convert your MRN, regardless of length (if less than 10) and convert it to a 10 digit number with leading zeroes.
Hope this helps…
Tom
Thx. I had to “re-read” the doc on the format command. I love the syntax docs, because they are so easy to read for us “non-programmer” types 🙄
Hi,
Input= 12.34
set xlateOutVals [format “%0.3f” [lindex $xlateInVals 0]]
Output=12.340
My question: How can i padded a zero at the left to have always on the Output 012.340
Thanks
Moe
format “0%0.3f” $val
Rob Abbott
Cloverleaf Emeritus
Thx.
-- Max Drown (Infor)
format “0%0.3f” $val
I need to format the Input to xxx.xxx (decimal lengh 7).
Example1: Input= 12 Output should be=012.120
Example2: Input=12.5 Output should be=012.500
format “0%0.3f” $val
I need to format the Input to xxx.xxx (decimal lengh 7).
Example1: Input= 12
-- Max Drown (Infor)
Thank you
Hi Max,
If my input is = 1.23 the Output wil be : 01.230 !!!! but me i need to get 001.230
Regards
That’s a good question. Maybe you could run the number through formatting twice? Once of the right side and once for the left side?
-- Max Drown (Infor)
Try changing Max’s original format code to:
set output [format “0%0.3f” $var]
That should give you what you need.
Hope this helps…
Tom Rioux
Why not:
format “00%0.3f” $stuff
where $stuff is your value?
email: jim.kosloskey@jim-kosloskey.com 30+ years Cloverleaf, 60 years IT – old fart.
That will always add two zeros to the left side (“0012.000”).
-- Max Drown (Infor)
This seems to work I think …
lappend input “12”
lappend input “12.15”
lappend input “1.23”
#xxx.xxx
foreach var $input {
[code]lappend input “12”
lappend input “12.15”
lappend input “1.23”
#xxx.xxx
foreach var $input {
-- Max Drown (Infor)
/beginCharlieMode
A single format command will provide the desired output:
format “%07.3f” 1.23
The 0 specifies leading zeros
The 7 specifies the TOTAL output field width including the decimal point (NOT just the number of digits to the left of the decimal)
The .3 specifies the number of places to the right of the decimal
The f specifies that this is a floating point number
/endCharlieMode
Cheers.
A single format command will provide the desired output:
format “%07.3f” 1.23
The 0 specifies leading zeros
The 7 specifies the TOTAL output field width including the decimal point (NOT just the number of digits to the left of the decimal)
The .3 specifies the number of places to the right of the decimal
The f specifies that this is a floating point number
Cheers.
That does appear to work perfectly. Thanks, Chris!
lappend input “12”
lappend input “12.15”
lappend input “1.23”
#xxx.xxx
foreach var $input {
set output [format “%07.3f” $var]
#set output [format “%0.3f” $var]
#set output [format “%07s” $output]
puts “$var: $output”
#Result:
#12: 012.000
#12.15: 012.150
#1.23: 001.230
}
-- Max Drown (Infor)