Fehler #12 » Unit.patch
newUnit.pm 2014-11-03 14:26:04.000000000 +0100 | ||
---|---|---|
44 | 44 |
return $self->{__base_factor}; |
45 | 45 |
} |
46 | 46 | |
47 |
sub can_convert_to { |
|
48 |
my ($self, $other_unit) = @_; |
|
49 |
foreach($self->convertible_units) |
|
50 |
{ |
|
51 |
my $element = $_; |
|
52 |
foreach(@$element) |
|
53 |
{ |
|
54 |
my $subelement = $_; |
|
55 |
if( $subelement->get_base_name() eq $other_unit->get_base_name() ) |
|
56 |
{ |
|
57 |
return 1; |
|
58 |
} |
|
59 |
} |
|
60 |
} |
|
61 |
return 0; |
|
62 |
} |
|
63 | ||
64 |
sub get_base_name{ |
|
65 |
my $self = shift; |
|
66 |
my $tmp = $self; |
|
67 |
while ($tmp->base) |
|
68 |
{ |
|
69 |
$tmp = $tmp->base; |
|
70 |
} |
|
71 |
return $tmp->name; |
|
72 |
} |
|
73 | ||
47 | 74 |
sub convert_to { |
48 | 75 |
my ($self, $qty, $other_unit) = @_; |
49 | 76 | |
50 | 77 |
my $my_base_factor = $self->base_factor || 1; |
51 | 78 |
my $other_base_factor = $other_unit->base_factor || 1; |
52 | ||
53 |
return $qty * $my_base_factor / $other_base_factor; |
|
79 |
|
|
80 |
if($self->can_convert_to($other_unit)) |
|
81 |
{ |
|
82 |
return $qty * $my_base_factor / $other_base_factor; |
|
83 |
} |
|
84 |
else |
|
85 |
{ |
|
86 |
return $qty; |
|
87 |
} |
|
54 | 88 |
} |
55 | 89 | |
56 | 90 |
1; |