Sponsored Content
Full Discussion: Significant Figures in awk
Top Forums Shell Programming and Scripting Significant Figures in awk Post 302614405 by Corona688 on Wednesday 28th of March 2012 12:46:51 PM
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:
 

7 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

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

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

6. 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

7. 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
ECVT(3) 						     Linux Programmer's Manual							   ECVT(3)

NAME
ecvt, fcvt - convert a floating-point number to a string. SYNOPSIS
#include <stdlib.h> char *ecvt(double number, int ndigits, int *decpt, int *sign); char *fcvt(double number, int ndigits, int *decpt, int *sign); DESCRIPTION
The ecvt() function converts number to a null-terminated string of ndigits digits (where ndigits is reduced to an system-specific limit determined by the precision of a double), and returns a pointer to the string. The high-order digit is nonzero, unless number is zero. The low order digit is rounded. The string itself does not contain a decimal point; however, the position of the decimal point relative to the start of the string is stored in *decpt. A negative value for *decpt means that the decimal point is to the left of the start of the string. If the sign of number is negative, *sign is set to a non-zero value, otherwise it's set to 0. If number is zero, it is unspecified whether *decpt is 0 or 1. The fcvt() function is identical to ecvt(), except that ndigits specifies the number of digits after the decimal point. RETURN VALUE
Both the ecvt() and fcvt() functions return a pointer to a static string containing the ASCII representation of number. The static string is overwritten by each call to ecvt() or fcvt(). NOTES
These functions are obsolete. Instead, sprintf() is recommended. Linux libc4 and libc5 specified the type of ndigits as size_t. Not all locales use a point as the radix character (`decimal point'). CONFORMING TO
SysVR2, XPG2 SEE ALSO
ecvt_r(3), gcvt(3), qecvt(3), setlocale(3), sprintf(3) 1999-06-25 ECVT(3)
All times are GMT -4. The time now is 07:14 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy