Gossamer Forum
Home : Products : DBMan : Customization :

strange fractions when converting date to unix and back

Quote Reply
strange fractions when converting date to unix and back
hi again

I am not sure about this

I am using the following code which is working fine but sometimes returns non whole numbers such as 16.34666667. It happens when the dates are more than about 20 days apart? Is 86400 the right number to divide by to get days?
Is there a way of rounding any result to whole numbers?

carl

Code:

sub modify_record {


my ($status, $line, @lines, @data, $output, $found, $restricted);

$compare1 = &date_to_unix($in{'date'});
$compare2 = &date_to_unix($in{'currentdate'});
$total = $compare2-$compare1;
$in{'food'} = $in{'food'} + $in{'addfood'} - ($total/86400);
Quote Reply
Re: [carlstevenson] strange fractions when converting date to unix and back In reply to
Taken from a thread in the FAQ under "Calculate":

You want to round it to a whole number?

In sub add_record in the db.cgi file, before

$status = &validate_record;

add

$in{'FieldName'} = sprintf("%.0f",$in{'FieldName'});

Replace FieldName (in both places) with the name of the field you want to round off.
Repeat the line as many times as necessary to include all the fields you want to round.

yes, 86400 is the right number for each day

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/