LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Round up to 2 decimal place with the character at last (https://www.linuxquestions.org/questions/linux-newbie-8/round-up-to-2-decimal-place-with-the-character-at-last-4175480753/)

Bunty2013 10-14-2013 12:40 PM

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???

Bunty2013 10-14-2013 01:08 PM

Any suggestion???

Robhogg 10-14-2013 01:24 PM

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'

Bunty2013 10-14-2013 01:32 PM

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.

danielbmartin 10-14-2013 01:34 PM

Quote:

Originally Posted by Bunty2013 (Post 5045558)
What I want as the output to be like this 123.46M
I want the "M" to be printed at last.

Try this:
Code:

echo 123.45567M | awk -F "" '{printf("%.2f%s\n",$0,$NF)}'
Daniel B. Martin

Bunty2013 10-14-2013 01:36 PM

Thanks daniel. I have one more question. could you look into that.

danielbmartin 10-14-2013 01:38 PM

Quote:

Originally Posted by Bunty2013 (Post 5045587)
Thanks daniel. I have one more question. could you look into that.

State the question. If it is distinct from post #1 start a new thread.

Daniel B. Martin

Bunty2013 10-14-2013 01:47 PM

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

danielbmartin 10-14-2013 01:49 PM

Quote:

Originally Posted by Bunty2013 (Post 5045587)
Thanks daniel. I have one more question. could you look into that.

Maybe this is what you seek...
Code:

  Path=$(cut -d'.' -f1 <<< ${0})
 InFile=$Path"inp.txt"
OutFile=$Path"out.txt"
awk -F "" '{printf("%.2f%s\n",$0,$NF)}' $InFile >$OutFile

This code snippet assumes you have all three files (InFile, OutFile, and the script itself) in the same folder.

Daniel B. Martin

danielbmartin 10-14-2013 01:53 PM

Quote:

Originally Posted by Bunty2013 (Post 5045593)
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

With this InFile ...
Code:

123.45567M
22.7867M
145.7689M

... this code ...
Code:

awk -F "" '{printf("%.2f%s\n",$0,$NF)}' $InFile >$OutFile
... produced this OutFile ...
Code:

123.46M
22.79M
145.77M

Daniel B. Martin

Bunty2013 10-14-2013 01:56 PM

Thanks Daniel.It was helpful to me.

Bunty2013 10-16-2013 11:04 PM

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.

danielbmartin 10-17-2013 06:49 AM

Quote:

Originally Posted by Bunty2013 (Post 5047263)
... 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.

With this InFile ...
Code:

123.45567M
22.7867M
145.7689M
123M

... this code ...
Code:

awk -F "" '{printf("%.2f%s\n",$0,$NF)}' $InFile |sed 's/.00M/M/' >$OutFile
... produced this OutFile ...
Code:

123.46M
22.79M
145.77M
123M

Daniel B. Martin

danielbmartin 10-17-2013 09:21 AM

Quote:

Originally Posted by Bunty2013 (Post 5047263)
... 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.

With this InFile ...
Code:

123.45567M
22.7867M
145.7689M
123M

... this awk ...
Code:

awk -F "" '{if (index($0,".")>0) printf("%.2f%s\n",$0,$NF)
            else print $0'} $InFile >$OutFile

... produced this OutFile ...
Code:

123.46M
22.79M
145.77M
123M

Daniel B. Martin

grail 10-17-2013 10:22 AM

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.