Joined: 11 Nov 2008
Location: United Kingdom
Online Status: Offline
Posts: 35
Topic: Even Integers Posted: 26 Jul 2010 at 11:12pm
Hi Guys,
I've been working on a crystal report that pulls down two fields that lists time spent on a job.
One field is hours, and one field is minutes.
The minute field will only ever be "0" or "30" (as in a half hour).
So i've written a sum that adds the totals of both and then adds a :
------------------------- The report currently looks like this:
1:30 2:00 1:00 1:30 1:30
Total: 6:90 Hours in Total.
So I need to write a formula that correctly calculates hours/ minutes in the Total (so it appears as 7:30).
Is there a function in Crystal reports to do this? or do I need to make my own formula? at the moment, i've started writing my own formula but have got stuck.
I've got a shared NumberVar (called HalfHour) that counts the times "30" appears in the group of the report so that I can calculate the Grand total of time spent.
So now I need to know how to check if the value is odd or even.
So what I was thinking was an If statement that does:
If shared NumberVar HalfHour > 1 then
(Check if value is odd or even) (do some maths to correctly display hours/ minutes) (write out to form)
Else (Carry on with rest of formula)
--------------------
Am I approaching this in the right way, or am I making it way to overly complicated?
Joined: 21 Dec 2007
Online Status: Offline
Posts: 4374
Posted: 27 Jul 2010 at 3:07am
I would try DATEADD, maybe starting with a set date at midnight, and then looking at the time portion to see how much has elapsed, or you might try converting the 30 to .5 when adding.
As for even an odd, I would try use something like x mod 2. if the number = 0 then it is even, 1 is odd. x is what you are testing.
Joined: 11 Nov 2008
Location: United Kingdom
Online Status: Offline
Posts: 35
Posted: 27 Jul 2010 at 4:27am
Hi,
Many thanks, i've amended the formula so its using mod2, but there is something wrong with the If Statement, as it breaks if I use Else (comes up with 'remaining text is not part of the formula'.
----------------------------------------------------- HalfHour = counts the number of times the minutes field comes in at '30' THours = (shared) Total number of hours in the hours in the report TMinutes = (shared) Total number of Minutes in the report
--------------------------------
if shared NumberVar HalfHour > 1 then
if HalfHour MOD 2 = 1 then
HalfHour := HalfHour - 1; // Odd number - so remove 1 for '30 mins' THours := THours + HalfHour; TMinutes := 30; // is an odd number, so we know there is a half hour
if HalfHour MOD 2 = 0 then
THours := THours + HalfHour; TMinutes := 0; // Even number so we know there are no half hours left over
// convert to string to remove decimal point StringVar GHours := ToText(shared NumberVar THours,0); StringVar GMinutes := ToText(shared NumberVar TMinutes,0);
// add : GHours & ":" & GMinutes;
---------------------------------
I know the maths isnt 100% correct in there yet - but I just want to get its sort of working and then refine it later.
Joined: 21 Dec 2007
Online Status: Offline
Posts: 4374
Posted: 30 Jul 2010 at 3:13am
if shared NumberVar HalfHour > 1 then
if HalfHour MOD 2 = 1 then ( HalfHour := HalfHour - 1; // Odd number - so remove 1 for '30 mins' THours := THours + HalfHour; TMinutes := 30; // is an odd number, so we know there is a half hour ) if HalfHour MOD 2 = 0 then ( THours := THours + HalfHour; TMinutes := 0; // Even number so we know there are no half hours
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot delete your posts in this forum You cannot edit your posts in this forum You cannot create polls in this forum You cannot vote in polls in this forum