Printf question: getting padded zero in decimal plus floating point together.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printf question: getting padded zero in decimal plus floating point together.
# 1  
Old 05-14-2013
Printf question: getting padded zero in decimal plus floating point together.

Hi Experts,
Quick question:

I am trying to get the output with decimal and floating point but not working:

Code:
echo "20.03" | awk '{printf "%03d.2f\n" , $0 }'
020.2f

How to get the output as :
Code:
020.03


Thank you.
# 2  
Old 05-14-2013
Quote:
Originally Posted by rveri
...
How to get the output as :
Code:
020.03

...
Code:
$
$ echo "20.03" | awk '{printf "%06.2f\n" , $0 }'
020.03
$
$

This User Gave Thanks to durden_tyler For This Post:
# 3  
Old 05-14-2013
You can also control numeric output format using gawk OFMT built-in variable:
Code:
$ echo "20.03" | awk 'BEGIN{OFMT="%06.2f"}{print $0+0}'
020.03

OR
Code:
$ awk 'BEGIN{OFMT="%06.2f";print 20.03}'
020.03

These 2 Users Gave Thanks to Yoda For This Post:
# 4  
Old 05-14-2013
For this simple example you don't need awk
Code:
$ printf "%06.2f\n" 20.03
020.03

These 2 Users Gave Thanks to MadeInGermany For This Post:
# 5  
Old 05-16-2013
Thanks all I got it now .

Code:
echo "20.03" | awk '{printf "%06.2f\n" , $0 }'
awk 'BEGIN{OFMT="%06.2f";print 20.03}'
printf "%06.2f\n" 20.03

All worked... Thanks.

---------- Post updated at 03:03 PM ---------- Previous update was at 02:59 PM ----------

>> For this simple example you don't need awk
- correct , Actually the program I was needing help for this was in awk.
Thanks ...
This User Gave Thanks to rveri For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printf padded string

Is possible to print padded string in printf? Example echo 1 | awk '{printf("%03d\n", $1)}' 001I want S1 S11 S2 S21to be padded as: S01 S11 S02 S21Thanks! (26 Replies)
Discussion started by: yifangt
26 Replies

2. Programming

Floating Point

Anyone help me i cant found the error of floating point if needed, i added the code complete #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> typedef struct { int hh; int mm; int ss; char nom; int punt; }cancion; typedef struct... (9 Replies)
Discussion started by: Slasho
9 Replies

3. Shell Programming and Scripting

floating point not recognized by printf in bash

Dear all, I have the following question. Let's say that I have the following script #!/bin/bash value=0.4987865 a=` printf "%6.2f" $value ` b=`echo $value + $value | bc -l` echo $a echo $b exit And the exit is: 0,00 .9975730 Thus, the problem is that the printf order does not... (2 Replies)
Discussion started by: josegr
2 Replies

4. Shell Programming and Scripting

Arithmetic in floating point

is it not possible to simply di aritmetic without using bc or awk i have tried folllowing operatrions but they support only integer types plz suggest me code for floating using values stored in the variables.the ans i get is integer and if i input floating values i get error numeric constant... (6 Replies)
Discussion started by: sumit the cool
6 Replies

5. Shell Programming and Scripting

floating point numbers in if

# if > then > echo "1" > else > echo "2" > fi -bash: How can i compare floating point numbers inside statement? (15 Replies)
Discussion started by: proactiveaditya
15 Replies

6. Shell Programming and Scripting

how to compare 2 floating point no.

Hi, Could any one tell me how to compare to floating point no. using test command. As -eq option works on only intergers. i=5.4 if then echo "equal" else echo "not equal" fi here output will be equal even though no. are unequal. Thanks, ravi (1 Reply)
Discussion started by: useless79
1 Replies

7. Programming

Floating point Emulator

what is floating point emulator(FPE)? where and why it is used? (1 Reply)
Discussion started by: pgmfourms
1 Replies

8. Linux

Floating point exception !!!

Hi, I have linux fedora 4 ver., 2.6 kernal. And qmail & mysql & samba servers are already configured on this server. When I try to install any package like squidguard ,dansguardian,webmin,rsnapshots with command rpm -ivh . It is giving error as “Floating point exception" Snap View is... (3 Replies)
Discussion started by: ssk01
3 Replies

9. Programming

Floating point error in C

Hi, see the simple code below double i; i=8080.9940; printf(" val :%.30f\n",i); output i m getting is val :8080.993999999999700000000000000 when i m expecting val :8080.9940 what happens?how can i avoid it? thanks... (2 Replies)
Discussion started by: Hara
2 Replies

10. Programming

floating point problem

Hi all! Hi all! I am working with a problem to find the smallest floating point number that can be represented. I am going in a loop ,stating with an initial value of 1.0 and then diving it by 10 each time thru the loop. So the first time I am getting o.1 which I wanted.But from the next... (4 Replies)
Discussion started by: vijlak
4 Replies
Login or Register to Ask a Question