Operating System - HP-UX
1751973 Members
5216 Online
108784 Solutions
New Discussion

round up (not to an integer)

 
Dan Copeland
Regular Advisor

round up (not to an integer)

Hello admins,

Looking for a way to round 0.31 to 0.40 or 0.02 to 0.10 etc. in a shell/perl script or some other method--Linux methods also accepted :-)

Everything that I’ve seen rounds up to an integer (e.g. ceil) or won’t round up if it’s less than or equal to “5” for the significant digit you are trying to round against…

tia,
Dan
15 REPLIES 15
Steven E. Protter
Exalted Contributor

Re: round up (not to an integer)

Shalom,

The term round up is defined as rounding up to an integer.

It eliminates fractions.

You might try moving the decimal point one to the right, rounding and then moving it back one digit to the left.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
OldSchool
Honored Contributor

Re: round up (not to an integer)

yep...you're gonna have to do some math...as in multiply the original value X 10, subtract the integer portion of the result from the result. if that result is positive, at one to the integer portion and divide that by 10 to get back to the correct decimal position.

thats not going to happen with shell variables, as they're integers anyway, so you'd need "bc" or ?

perl / awk might work as well
Frank de Vries
Respected Contributor

Re: round up (not to an integer)

If you have an oracle db ,
You can use the function round()
in Oracle and specify how far you want to round. One decimal , two decimal etc..

SQL> select round(0.05,1) from dual;

ROUND(0.05,1)
-------------
.1


You can script this in shell where you call
sqlplus <
EOF


regards,

Look before you leap
Dennis Handly
Acclaimed Contributor

Re: round up (not to an integer)

>Looking for a way to round 0.31 to 0.40 or 0.02 to 0.10 etc. in a shell/perl script or some other method

Basically you want to round up (or away from zero) to tenths.
A shell will have problems since it only handles integers, unless you have ksh93.

>Everything that Iâ ve seen rounds up to an integer (e.g. ceil) or wonâ t round up if itâ s less than or equal to 5 for the significant digit you are trying to round against

You could be using IEEE rounding where you can select 4 rounding modes. Or with decimal floating point, you have 5 modes:
http://h21007.www2.hp.com/portal/site/dspp/menuitem.863c3e4cbcdc3f3515b49c108973a801/?ciid=8cf166fedd1aa110VgnVCM100000a360ea10RCRD#A.6

>SEP: The term round up is defined as rounding up to an integer.

That's only one definition. You can also round to the nearest penny, .01. COBOL has had ROUNDED for decades and it applies to fixed point arithmetic.

>OldSchool: you're gonna have to do some math

Or complicated string manipulation.
Or use awk for the arithmetic.

You could use the shell to first separate the integral vs fractional parts, left to user.
typeset -i int frac
For positive values:
(( frac = (frac + 9) / 10 * 10 ))
If (( frac >= 100 )); then
(( int +=1 ))
(( frac -= 100 ))
fi

Then glue the two parts together.
Bob E Campbell
Honored Contributor

Re: round up (not to an integer)

I was in a Rube Goldberg frame of mind, so rather than using Python, Perl, C, or even dc(1) I submit the following:

sed -e "s/\.$/.0/" -e "/./ s/\([0-9]*\)\.\([0-9]\)\([0-9]*\)/\1\2X\3/" | \
while read X
do
echo $X | grep -sq -e "X"
if [[ $? -eq 0 ]]
then
integer lVal=${X%X*}
integer rVal=${X#*X}
if [[ $rVal -gt 0 ]]
then
(( lVal += 1 ))
fi
echo "${lVal}."
else
echo "$X"
fi | sed -e "/./ s/\([0-9]*\)\([0-9]\)\./\1.\2/"
done


When passed:
0
1.234
.57
88.
89.0

It returns:
0
1.3
.6
88.0
89.0


Sorry about the 88. becoming 88.0, but I was just doing this from a shell weenie perspective. You need real floating point support. Easy to adjust for precision and the real sickos could make it all an eval.
Dennis Handly
Acclaimed Contributor

Re: round up (not to an integer)

>Bob: real sickos ..

Or using COBOL:
01 result PIC 9(9).99.
01 trunc PIC 9(9)V9.
01 input-val PIC 9(9)V99.
add .09 input-val giving trunc
move trunc to result

James R. Ferguson
Acclaimed Contributor

Re: round up (not to an integer)

Hi:

> Dennis: Or using COBOL

OMG, you don't really, do you?!? I thought you only cared about the first letter :-))

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: round up (not to an integer)

>JRF: I thought you only cared about the first letter :-))

I supported COBOLII on MPE for 14+ years.
Michael Mike Reaser
Valued Contributor

Re: round up (not to an integer)

JRF:

>> Dennis: Or using COBOL

>OMG, you don't really, do you?!? I thought you only cared about the first letter :-))

Back in the Dim Past when I was at the Atlanta RC, it seemed that Dennis's fingers could be found on and around pretty much any and all of the compilers from any and all of the platforms. Except maybe BASIC and Business Basic on the classic 3000's. *Maybe*. :-) :-) :-)
There's no place like 127.0.0.1

HP-Server-Literate since 1979