- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- round up (not to an integer)
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2008 06:41 AM
11-19-2008 06:41 AM
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--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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2008 07:15 AM
11-19-2008 07:15 AM
Re: round up (not to an integer)
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
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2008 07:21 AM
11-19-2008 07:21 AM
Re: round up (not to an integer)
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2008 07:30 AM
11-19-2008 07:30 AM
Re: round up (not to an integer)
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,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2008 10:22 AM
11-19-2008 10:22 AM
Re: round up (not to an integer)
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2008 10:37 AM
11-19-2008 10:37 AM
Re: round up (not to an integer)
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2008 01:28 PM
11-19-2008 01:28 PM
Re: round up (not to an integer)
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2008 01:31 PM
11-19-2008 01:31 PM
Re: round up (not to an integer)
> Dennis: Or using COBOL
OMG, you don't really, do you?!? I thought you only cared about the first letter :-))
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2008 02:00 PM
11-19-2008 02:00 PM
Re: round up (not to an integer)
I supported COBOLII on MPE for 14+ years.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2008 02:04 PM
11-19-2008 02:04 PM
Re: round up (not to an integer)
>> 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*. :-) :-) :-)
HP-Server-Literate since 1979
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2008 02:29 PM
11-19-2008 02:29 PM
Re: round up (not to an integer)
> Dennis: I supported COBOLII on MPE for 14+ years.
Impressive! Many years ago, I had occassion to write a small a patch to what was then the Burroughs COBOL68 compiler, in order to make life easier for our application programmers.
The now defunct compiler was a very large piece of ALGOL which implemented COBOL on a stack machine architecture. It had some "ugly" things it did to do so in a very elegant way.
Someone had included a comment in the source code for the compiler stating:
"Lasciate ogni speranza voi ch'entrate"
...which simply translates:
"Abandon all hope ye who enter here"
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2008 07:10 PM
11-19-2008 07:10 PM
Re: round up (not to an integer)
Hmmm, do we have APL on HP-UX???
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2008 09:06 PM
11-19-2008 09:06 PM
Re: round up (not to an integer)
Uh... just use printf?
What am I missing here?
$ perl -e 'printf qq(%5.1f\n), 0.05+shift' 0.31
0.4
$ perl -e 'printf qq(%5.1f\n), 0.05+shift' 0.02
0.1
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2008 10:04 PM
11-19-2008 10:04 PM
Re: round up (not to an integer)
alp $ perl -e "printf qq(%5.1f\n), 0.05+shift" 0.40
0.5
I assume that this is undesired. Perhaps
something lame like "0.0499999" instead of
"0.05" would be better, but I wouldn't trust
it.
> The term round up is defined as rounding
> up to an integer.
Some of us need to get out more.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2008 10:13 PM
11-19-2008 10:13 PM
Re: round up (not to an integer)
perl -e 'printf qq(%5.1f\n), 0.05+shift' 0.40
0.5
(Rats: Steven had the same example.)
And you don't have a trailing "0". :-)
You need to round UP, not do IEEE rounding (which does odd/even on exactly .5) on N +.5.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2008 04:39 AM
11-20-2008 04:39 AM
Re: round up (not to an integer)
Yes, that's what I meant, but I rounded the number :-)
perl -e 'printf qq(%5.1f0\n), 0.04999+shift' 0.4
Also notice the bonus "0". Yuck.
:^)
Hein.