: Is there any command to round off a number? Like if the
: input is 1.234, I want the output to be 1.23, but if the
: input is 1.236, I want the output to be 1.24 etc.
This should do it.
$ printf "%.2f\n" 1.234
1.23
$ printf "%.2f\n" 1.236
1.24
>Is there any command to round off a number? Like if the
>input is 1.234, I want the output to be 1.23, but if the
>input is 1.236, I want the output to be 1.24 etc. If there is
>no standard UNIX command to do this can anyone provide a simple
>shell script to do this? Thanks in advance.
Most shells don't do floating point math (are there any that do?). Thus you
are reduced to using awk/bc/dc. Here is an awk solution:
awk 'BEGIN{PREC=2} {printf"%0.PREC"f\n", $1}'
PREC gives the number of digits to the right of the decimal point. This reads
one number per line from standard input and writes the rounded off number to
standard output. You should also probably check to make sure your version of
awk rounds floating point numbers, rather than truncating them. All the ones
I have access to behave in that way.
Regards,
Gora
Thanks. This nice. But it assumes rounding to 2 deciaml places. What
I had in mind was more general. like it would convert 1.23456 to 1.2346
etc ie rounding off to next lower decimal place. I want to use this tool
in a script where I may not know the # of decimal places.
>In <1995Jul17.0...@lmpsbbs.comm.mot.com> masu...@tr729.tr.comm.mot.com (Masudur Rahman) writes:
Sorry to follow up to my own posting, but there is a typo in it. The line
>awk 'BEGIN{PREC=2} {printf"%0.PREC"f\n", $1}'
should actually read
awk 'BEGIN{PREC=2} {printf"%0."PREC"f\n", $1}'
(Note the extra double-quote between 0. and PREC in the printf statement.) My
apologies for the mistake.
Regards,
Gora
OK, then perhaps this will do the job. It fails on the ".123456". I
don't know much about printf; it doesn't seem to like numbers that begin
with a decimal point.
#!/bin/ksh
function one_less_decimal
{
FRACTION=`print $1 | sed 's/^.*\.//'` # Delete everything through dec. pt.
let DECIMALS=${#FRACTION} # Number of remaining characters.
let ROUND=DECIMALS-1
if [ $ROUND -lt 0 ]; then
let ROUND=0
fi
printf "%.${ROUND}f\n" $1
}
one_less_decimal 1.23456
one_less_decimal 0.123454
one_less_decimal 0.123456
one_less_decimal .123456
Thanks for your response. First I am lucked out. My awk (HP/UX) doesnt sem
to support rounding, I got syntax error. Second this script is hardcoded for
two digit rounding. What I had in mind was a more general tool/script to round
of a number to one less decimal postion. e.g 1.23456 to 1.2346 etc. since my
intent is to use this tool in a script to act on a variable I may not know
the number of decimal place ahead of time. Thanks again.
>Is there any command to round off a number? Like if the
>input is 1.234, I want the output to be 1.23, but if the
>input is 1.236, I want the output to be 1.24 etc. If there is
>no standard UNIX command to do this can anyone provide a simple
>shell script to do this? Thanks in advance.
This is not tested but I think it works. Formatting the output is a
hassle so the printf actualy puts a space before the newline.
echo $1| \
awk 'BEGIN {FS="."} {
decs = 10^length($2)
printf("%-f \n", int(($0 * decs + 5) / 10) / decs * 10)
}'
======================================================
"My boss says we need some eunuch programmers."
"I think he means UNIX and I already know UNIX."
"Well, if the company nurse comes by, tell her
I said never mind."
- Dilbert -
======================================================
Warren Stott war...@rmii.com
W. Stott & Assoc. Systems, Software, Support
(303) 666-0499 Superior, CO
>
> Hmm, how about this:
>
> #!/usr/bin/awk -f
> { printf "%." length($1)-index($1,".")-1 "f\n", $1 }
>
> Thus, the format string is constructed online. The script eats one
> number per line, without fancy checkings.
>
Great!, this is the shortest script so far! I am not surprised since
you are a physicist! regards.
> --
> Michael Sternberg, Physics Research Scholar m...@po.cwru.edu
> Case Western Reserve University Lab phone: (216) 368 4034
> "Who disturrrbs me at this time?" << Zaphod Beeblebrox IV >>
> : Is there any command to round off a number? Like if the
> : input is 1.234, I want the output to be 1.23, but if the
> : input is 1.236, I want the output to be 1.24 etc.
> This should do it.
> $ printf "%.2f\n" 1.234
> 1.23
> $ printf "%.2f\n" 1.236
> 1.24
What about 1.235???? I know a perl script which can do the same thing, it
works with 1.234 and 1.236, but it gives me 1.23 when i input 1.235
>Thanks. This nice. But it assumes rounding to 2 deciaml places. What
>I had in mind was more general. like it would convert 1.23456 to 1.2346
>etc ie rounding off to next lower decimal place.
Hmm, how about this:
#!/usr/bin/awk -f
{ printf "%." length($1)-index($1,".")-1 "f\n", $1 }
Thus, the format string is constructed online. The script eats one
number per line, without fancy checkings.
--
echo $1|awk '{n=split($1,a,".");PREC=length(a[n])-1;printf"%0."PREC"f\n",$1}
We use X-Windows Netscape.
Would somebody help us and answer to the following question :
We have html format file which contains different URL links and
would like to bring and save on our machine all files (from URL links)
automatically in the loop (!) using the script (without clicking by mouse).
So, the input will be html file with links ,the output will be linked files
saved in our home directory.
Please help us and thank you in advance.