Re: Sum not recoginzing a negative number
From: Allen Browne (AllenBrowne_at_SeeSig.Invalid)
Date: 12/06/04
- Next message: jkendrick75: "Re: incorrect sums in report using 2 tables"
- Previous message: Tony Williams: "Running report from input form"
- In reply to: Bobk: "Re: Sum not recoginzing a negative number"
- Next in thread: Bobk: "Re: Sum not recoginzing a negative number"
- Reply: Bobk: "Re: Sum not recoginzing a negative number"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 7 Dec 2004 00:20:54 +0800
Using your example in the Immediate Window:
? CDate(38327.06278935), CDate(-38327.06278935)
12/6/2004 1:30:25 AM 01/22/1795 1:30:25 AM
Now subtract 3 hours (0.125 of a day). You expect the result to be
10:30:25pm on the day before in both cases? Try it:
? CDate(38327.06278935 - .125), CDate(-38327.06278935 - .125)
You cannot treat date/times as floating point numbers, particularly with
negative values.
-- Allen Browne - Microsoft MVP. Perth, Western Australia. Tips for Access users - http://allenbrowne.com/tips.html Reply to group, rather than allenbrowne at mvps dot org. "Bobk" <Bobk@discussions.microsoft.com> wrote in message news:004B0C77-DBEC-46E8-B5BB-703E0D3D4F0E@microsoft.com... > Thanks for the response. I thought that CDbl was supposed to result in a > double precision number, which it appears to do. My particular example has > a > date of 12/6/2004 2:23:35 AM for the end time and 12/6/2004 1:30:25 AM for > a > start time. > After I convert these times to Double precision I get 38327.099710648 for > the end time and 38327.06278935 for the start time. Multiplying by -1 > yields > -38327.06278935. Adding these together yields a difference of .036921298. > This multiplied by 24 =.886111152 which is equal to the hours difference, > which converts to 53 minutes and 10 seconds. If this works so well when I > compute it manually it should work in the report. The only problem I see > is > that Sum doesn't recognize my negative value and just goes ahead and adds > it > as though it were a positive number. Why? > > "Allen Browne" wrote: > >> You cannot use normal floating point math with date/time values. >> >> Open the Immediate window (Ctrl+G), and enter: >> ? CDate(0.25) >> As expected, a quarter of a day is 6am - actually 6am on Dec 30 1899. >> >> So what would you expect from: >> ? CDate(-0.25) >> Would you expect that to be 6pm on the day before? >> It may surpris you to learn that it is 6am on Dec 30 1899 also. >> >> The reason is that date/time values are defined like this: >> - the integer part represents the date, and >> - the fraction part represents the time. >> >> In the example above, the zero represents the date (Dec 30, 1899), and >> the >> 0.25 represents the time. Applying the same logic to -0.25, the -0 >> represents the date, and the 0.25 represents the time. Since 0 is the >> same >> as -1, the date is the same. And since 0.25 is the same in both cases, >> 0.25 >> and -0.25 both represent the same date and time. >> >> The upshot is that you should always use the built-in functions such as >> DateDiff() when working with dates and times. Normal floating-point math >> does not apply. >> >> -- >> Allen Browne - Microsoft MVP. Perth, Western Australia. >> Tips for Access users - http://allenbrowne.com/tips.html >> Reply to group, rather than allenbrowne at mvps dot org. >> >> "Bobk" <Bobk@discussions.microsoft.com> wrote in message >> news:D72B7100-0AFD-448D-AA3A-C0A45B3E3009@microsoft.com... >> >I have a report in which I am attempting to calculate elapsed time. One >> >being >> > the start of work on a job, the second being the completion of the job. >> > The >> > start time and the end time are entered through a form which creates a >> > record >> > for each event. In my report I group the two events by day and sum them >> > using >> > the sum function. In order to force subtraction (subtracting the >> > starting >> > time from the ending time) I multiply the beginning time by -1 which >> > results >> > in a minus number. When the report adds the two it does not recognize >> > the >> > negative number and sums them together as if they were both positive. I >> > use >> > the following routine to calculate the negative value: IIf([Start]="Y", >> > ((CDbl([Trak_Date_Time]))*-1), CDbl([Trak_Date_Time])) >> > Where: Trak_Date_Time is date/time. I use CDbl to convert date/time to >> > a >> > double precision value and multiply by minus 1 to get a negative number >> > for >> > the stating time.If I manually subtract the converted sarting time from >> > the >> > end time I get the correct elapsed time. On my report the the end time >> > appears as a positive and the start time appears as a -. Summing the >> > two >> > should result in elapsed time. Sum does not recognize the negative >> > value >> > and >> > adds both together disregarding the minus sign. I'm at a loss. Anyone >> > have >> > a >> > suggestion? >> >> >>
- Next message: jkendrick75: "Re: incorrect sums in report using 2 tables"
- Previous message: Tony Williams: "Running report from input form"
- In reply to: Bobk: "Re: Sum not recoginzing a negative number"
- Next in thread: Bobk: "Re: Sum not recoginzing a negative number"
- Reply: Bobk: "Re: Sum not recoginzing a negative number"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|
Loading