Add leading zeros in floating point variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add leading zeros in floating point variable
# 1  
Old 03-02-2011
Add leading zeros in floating point variable

I need to add leading zeros in a floating point numbers.
The length of the number should be 13 including decimal.
The input number is changing so number of leading zeros is not fix.

For example
input output
216.000 000000216.000
1345.000 000001345.000
22345.500 000022345.500

How can i do this in bash shell scripting.
kindly suggest.

Thanks in advance.
# 2  
Old 03-02-2011
Code:
awk '{printf "%013.3f\n" ,$1}' infile

000000216.000
000001345.000
000022345.500

# 3  
Old 03-02-2011
Thanks for the quick reply.
But i am passing these values to a function and there i used the awk command mentioned by you(pay_amount=`awk '{printf "%013.3f\n" ,$1}'$4 `) ,its showing
awk: syntax error near line 1
awk: bailing out near line 1

Kindly suggest
# 4  
Old 03-02-2011
Quote:
Originally Posted by reeta_shri
Thanks for the quick reply.
But i am passing these values to a function and there i used the awk command mentioned by you(pay_amount=`awk '{printf "%013.3f\n" ,$1}'$4 `) ,its showing
awk: syntax error near line 1
awk: bailing out near line 1

Kindly suggest
Use nawk or /usr/xpg4/bin/awk on Solaris.
# 5  
Old 03-02-2011
Another way would be..
Code:
cat inputfile | xargs printf "%013.3f\n"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Numbers with leading zeros

Hi, i have a variable which conatins values like 00001,0003,00067,00459. I want to use the values one by one and in the same form as they are like 00001,0003,00067,00459. Also can anyone tell me how to increment those numbers by 1,keeping the format as same like 00002,0004,00068,00460.... (5 Replies)
Discussion started by: arijitsaha
5 Replies

2. Shell Programming and Scripting

IF statement on floating point variable in scientific format

Hi all, I'm struggling :wall: to make a an if statement on two variables in scientific format. e.g. a=1.23e+01 b=3.21e+02 I know that it can be done with bc if the variable are not in scientific format. But what to do in this case??? Thank you, (3 Replies)
Discussion started by: f_o_555
3 Replies

3. UNIX for Dummies Questions & Answers

Add leading zeros to columns in a file

Hello Gurus, Quick question. I have a file with the following records: A~000000000000518000~SLP ~99991231~20090701~88.50~USD~CS~ A~000000000000518000~SLP ~99991231~20090701~102.00~USD~CS~ A~000000000000772000~SLP ~99991231~20100701~118.08~USD~CS~ I wold like to do the following: 1. Add... (1 Reply)
Discussion started by: chumsky
1 Replies

4. UNIX for Dummies Questions & Answers

Add floating point numbers from file

How do I use bash to add all the floating point numbers saved in a file like this? 490.47 244.61 263.07 131.59 246.81 115.20 (3 Replies)
Discussion started by: locoroco
3 Replies

5. Shell Programming and Scripting

Floating point to integer in variable length lines

Hi ! I'm looking for a way to transform certain floating point numbers in a one-line, variable length file to integers. I can do this in a crude way with sed : sed -e 's/0\.\(\):/\1:/g' -e 's/0\.0\(\):/\1:/g' -e 's/1\.000:/100:/g' myfile ... but this doesn't handle the rounding correctly. ... (3 Replies)
Discussion started by: jossojjos
3 Replies

6. Shell Programming and Scripting

Removing leading zeros from a variable

How do I remove or add leading zeroa from a variable. To make variable 10 characters long when adding zeros. (6 Replies)
Discussion started by: toshidas2000
6 Replies

7. Shell Programming and Scripting

floating point shell variable

I have a two files >cat file1 jjjjj 10.345 6.673 ppp 9.000 5.883 >cat file2 mmm 80 10 jjjjj 10.305 6.873 ppp 9.000 5.883 I am reading file 1 line by line , and look for the string jjjj and then read the line in file 2 with jjjj I want to get the... (5 Replies)
Discussion started by: jojan
5 Replies

8. Shell Programming and Scripting

how to retain leading zeros

Hi All, I am working with a fixed width file Forrmat. C1 Number (10,3) C2 Number (10,3) e.g. c1= 0000000100.000 c2= 0000000020.000 0000000100.0000000000020.000 I have to perform c1 - c2 . i.e. I want answer to be 0000000080.000. but I am loosing the leading zeros( only getting... (3 Replies)
Discussion started by: Manish Jha
3 Replies

9. Shell Programming and Scripting

Leading zeros

How to insert leading zeros into a left-justisfied zip code? e.g. Zip code is written as 60320 which is left-justified to make it be read as 0060320. We have to move it to right-justifiable then insert 2 leading zeros into it... ;) (1 Reply)
Discussion started by: wtofu
1 Replies

10. Shell Programming and Scripting

floating point variable in UNIX script

HI all, Can someone help me how to use or assign a floating point value in a variable in a unix script. I dont know how to use it. for example, I am to assign a variable VAR1 a value of 2.34 and evaluate if a certain vaue is greater or less than to it. That is, if var1 > 2.13 will do... (7 Replies)
Discussion started by: darry
7 Replies
Login or Register to Ask a Question