View previous topic :: View next topic
|
Author |
Message |
nallaperumal
New User
Joined: 19 Nov 2007 Posts: 23 Location: Chennai
|
|
|
|
Hi
I just want to Round of the Dollar Amount to its nearest 10 cents.
Say if i have $ 7.23 then i need to round off to $ 7.20
if i have $ 7.26 then i need to round off to $ 7.30
if i have $ - 7.26 then i need to round off to $ 7.30
if i have $ - 7.24 then i need to round off to $ 7.20
Rounded function in cobol is not working. Please help me is there any way to solve it. |
|
Back to top |
|
|
Akatsukami
Global Moderator
Joined: 03 Oct 2009 Posts: 1788 Location: Bloomington, IL
|
|
|
|
You can use the LE functions CEExNWN (doing the scaling yourself) to get the rounded number, and CEExABS to drop the sign.
There's probably a much simpler way to do it with COBOL intrinsic functions, but as I'm not a COBOL programmer I don't know what it is. |
|
Back to top |
|
|
nallaperumal
New User
Joined: 19 Nov 2007 Posts: 23 Location: Chennai
|
|
|
|
Can any one help me to do it in COBOL. |
|
Back to top |
|
|
Nic Clouston
Global Moderator
Joined: 10 May 2007 Posts: 2455 Location: Hampshire, UK
|
|
|
|
Rounded function in COBOL IS working but it perhaps does not do what you want which is another thing all together. |
|
Back to top |
|
|
nallaperumal
New User
Joined: 19 Nov 2007 Posts: 23 Location: Chennai
|
|
|
|
Yes Rounded function is working, But not for my Requirement |
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
Code: |
01 W-VALUE-TO-DISPLAY COMP-3 PIC S9(8)V9.
|
Use something with a single decimal place:
Code: |
COMPUTE W-VALUE-TO-DISPLAY ROUNDED = your-2-decplace-field + 0 (or whatever calculation you have)
MOVE W-VALUE-TO-DISPLAY TO your-display-field
|
|
|
Back to top |
|
|
don.leahy
Active Member
Joined: 06 Jul 2010 Posts: 765 Location: Whitby, ON, Canada
|
|
|
|
It's actually very simple. Here is the explanation of how rounding works from the Cobol Language Reference:
After decimal point alignment, the number of places in the fraction of the result of an arithmetic operation is compared with the number of places provided for the fraction of the resultant identifier. When the size of the fractional result exceeds the number of places provided for its storage, truncation occurs unless ROUNDED is specified. When ROUNDED is specified, the least significant digit of the resultant identifier is increased by 1 whenever the most significant digit of the excess is greater than or equal to 5.
Study this for a while and the answer will become obvious. |
|
Back to top |
|
|
nallaperumal
New User
Joined: 19 Nov 2007 Posts: 23 Location: Chennai
|
|
|
|
Thanks my code is working fine.
Thanks Don and Bill Woodger |
|
Back to top |
|
|
|