Significant Figures in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Significant Figures in awk
# 1  
Old 03-28-2012
Significant Figures in awk

Hello,
I've had a look at the printf modifiers, but so far I haven't had much luck in making it do what I need.
I need to make it output with a set number of characters every time: whether it's by adding zeros at the end or by rounding, the output has to have the same number of characters in it, for example 6.
If it reads 10.1, 1, 0.1 and 0.01, the outputs would have to be 10.100 (the period counts), 1.0000, 0.1000 and 0.0100.
Is there a flag to do this?
# 2  
Old 03-28-2012
You can specify the number of places after the decimal point, but not the number of digits in general.

What I'd do is print it to more decimal places than you need into a variable, then substr() the variable to get the number of characters you want.

Code:
$ echo "10.3" | awk '{ V=sprintf("%.10f", $1); $1=substr(V, 0, 6); } 1'
10.300

$

Of course, that means you may end up with numbers like "10000." And if your number starts being so large the decimal point isn't included in the substr, the trick will no longer work.

Also: floating point numbers are not infinite-precision. If you look to enough decimal places you may find minute imprecision in the results of arithmetic.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 03-28-2012
It's ok, it's not really necessary, it was mostly for cosmetic reasons. Thanks anyway!
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with change significant figure to normal figure command

Hi, Below is my input file: Long list of significant figure 1.757E-4 7.51E-3 5.634E-5 . . . Desired output file: 0.0001757 0.00751 0.00005634 . . . (10 Replies)
Discussion started by: perl_beginner
10 Replies

2. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

3. Shell Programming and Scripting

Print a number up to last significant digit after decimal point

I want to write/print a number through a shell script up to its last significant digit (LSD) after the decimal point. Say, x=10.00056000000000000 I want to print x as x=10.00056. Note that x can be any thing so I cannot know the position of the LSD always. Thanks. (16 Replies)
Discussion started by: hbar
16 Replies

4. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

5. Shell Programming and Scripting

shell arithmetic least significant place

I need help on arithmetic root@server # hour=`date | awk {'print $4'} | cut -d: -f 1`; echo $hour 04 Now I subtract this result by 1 or 01 I get "3" as the answer. I need "03" as the answer, ie last two significant numbers should be there. root@server # hour=`date | awk {'print $4'} | cut... (3 Replies)
Discussion started by: anilcliff
3 Replies

6. Homework & Coursework Questions

C++ output with significant digits

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: As I stated in a previous thread, my assignment is to create a table of calculated data for the U.S. standard... (3 Replies)
Discussion started by: ds7202
3 Replies

7. UNIX for Dummies Questions & Answers

ls command help, figures not matching up

Hello all. I ran my AV app. and it scanned 600k+ files on my osx tiger powerpc laptop. I went into terminal and ran ls -l */* on my applications folder. for one entry it returned: Snood3.0/: total 17376 -rwxrwxrwx 1 ds admin 5152 Jan 17 2006 License30.txt -rwxrwxrwx 1 ds ... (2 Replies)
Discussion started by: jopper
2 Replies
Login or Register to Ask a Question