Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to round decimal places to nearest half? 1

Status
Not open for further replies.

sprog

IS-IT--Management
Sep 12, 2002
48
NZ
I have written a basic formula which returns a value with two decimal places. I need to round this value down to the nearest whole number if it is less than .5 and down to .5 if it is more than .5. (eg: 10.2 should be 10 and 10.7 should be 10.5).

Any help would be most appreciated.
Thanks
Julie
 
Try this:

int({Field}) + If remainder({Field},int({Field})) in .01 to .49 then 0 else .5


Software Training and Support for Macola, Crystal Reports and Goldmine
251-621-8972
dgilsdorf@mchsi.com
 
Thank you for your reply. I am using version 7 of crystal reports and cannot find a meaning for "int"? When I enter the formula, it says that "the remaining text does not appear to be part of the formula". Leaving the cursor at the beginning of the formula.


 
Try this

@RoundingToHalf

whileprintingrecords;
numbervar result;

result := truncate({table.numfield});
if {table.numfield} - result >= 0.5 then
result := result + 0.5;

result;

then set the numeric value of this formula field to whatever number of decimals you wish

hope this helps.
Jim Broadbent
 
Int means integer. Truncate should work as well. See if you have the truncate() function available. Software Training and Support for Macola, Crystal Reports and Goldmine
251-621-8972
dgilsdorf@mchsi.com
 
Dgillz - I would not use INT even if it was available since this function by itself will perform a rounding to the nearest whole number.

Also the use of remainder is for division not subtraction...you just want to extract the decimal portion of a number...not create another one

Jim Broadbent
 
Jim,

I understand what int() and remainder() do, and my formula works. What is your point? Software Training and Support for Macola, Crystal Reports and Goldmine
251-621-8972
dgilsdorf@mchsi.com
 
Jim,

As a followup, Int() does not round to the nearest whole number, it truncates, at least on positive numbers. Software Training and Support for Macola, Crystal Reports and Goldmine
251-621-8972
dgilsdorf@mchsi.com
 

ok...I won't argue. On reflection your method does work ,although it is not easy to sort out on first glance...in both of our approaches a decision would have to be made as how to treat negative numbers.
Jim Broadbent
 
Thank you both very much for your help. I have used your method, Jim and it has worked!!!!!

I have spent 2 weeks trying to do this so am most greatful. This is the first time I have used this site, and am really impressed.

Thanks once again.
Julie
 
Why all the messing about? Just double the number, truncate it and half it again!!

i.e. (Truncate (X * 2)) / 2



 
basil3legs - interesting approach ...I think this may even handle negative numbers correctly...thanks for sharing Jim Broadbent
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top