Round up to 2 decimal place with the character at last
How to truncate upto 2 decimal place & keep the character at last in linux?
I am using the below command. echo 123.45567M | awk '{printf("%.2f\n", $1)}' Output 123.46 What I want as the output to be like this 123.46M I want the "M" to be printed at last. Any suggestions??? |
Any suggestion???
|
Patience is a virtue? I saw your update just as I was about to post a suggestion, and nearly didn't...
Anyway, I would tend to use perl, rather than awk for something like this. In which case this works: echo 123.45567M | perl -ne '($n,$u) = $_ =~ /([0-9.]*)([A-Z]*)/; printf "%0.2f%s\n",$n,$u' |
Thanks Robhogg for the help.One last question, can i use this script to fetch the data from TXT file and give the output.If yes, where should i be mentioning the file name.
|
Quote:
Code:
echo 123.45567M | awk -F "" '{printf("%.2f%s\n",$0,$NF)}' |
Thanks daniel. I have one more question. could you look into that.
|
Quote:
Daniel B. Martin |
can i use this script to fetch the data from TXT file and give the output.If yes, where should i be mentioning the file name.
suppose file name abc.txt Data in the file contained 123.45567M 22.7867M 145.7689M Can i use the script (echo 123.45567M | awk -F "" '{printf("%.2f%s\n",$0,$NF)}') suggestion by you and generate the output 123.46M 22.79M 145.77M |
Quote:
Code:
Path=$(cut -d'.' -f1 <<< ${0}) Daniel B. Martin |
Quote:
Code:
123.45567M Code:
awk -F "" '{printf("%.2f%s\n",$0,$NF)}' $InFile >$OutFile Code:
123.46M |
Thanks Daniel.It was helpful to me.
|
I am using this script as suggested above by Daniel.It is working fine , but in some cases it is not.
The script echo 123.45567M | awk -F "" '{printf("%.2f%s\n",$0,$NF)}' Output 123.46M If i am mentioning the below numbers in the script I am getting this output. echo 123M | awk -F "" '{printf("%.2f%s\n",$0,$NF)}' Output 123.00M Can anyone please suggest me if there is a number 123M in the script then it should print 123M only not 123.00M. And if there is a number with decimal like 123.45678M then it should print 123.46M. I want the both things in one script. |
Quote:
Code:
123.45567M Code:
awk -F "" '{printf("%.2f%s\n",$0,$NF)}' $InFile |sed 's/.00M/M/' >$OutFile Code:
123.46M |
Quote:
Code:
123.45567M Code:
awk -F "" '{if (index($0,".")>0) printf("%.2f%s\n",$0,$NF) Code:
123.46M |
I thought your original had more promise Daniel ;)
Code:
awk -F "" '{f="%"(/\./?".2":".0")"f%s\n";printf f,$0,$NF}' file |
All times are GMT -5. The time now is 08:42 AM. |