| 1 | |||
| 2 | ### US Dollars | ||
| 3 | |||
| 4 | package Data::Quantity::Finance::Currency::USD; | ||
| 5 | |||
| 6 | use Data::Quantity::Finance::Currency; | ||
| 7 | @ISA = 'Data::Quantity::Finance::Currency'; | ||
| 8 | |||
| 9 | use Class::MakeMethods ( | ||
| 10 | 'Standard::Inheritable:scalar' => 'symbol', | ||
| 11 | 'Standard::Inheritable:scalar' => 'fixed_decimal', | ||
| 12 | ); | ||
| 13 | |||
| 14 | Data::Quantity::Finance::Currency::USD->scale('US Dollars'); | ||
| 15 | Data::Quantity::Finance::Currency::USD->symbol('$'); | ||
| 16 | Data::Quantity::Finance::Currency::USD->fixed_decimal(2); | ||
| 17 | |||
| 18 | Data::Quantity::Finance::Currency::USD->scale(); | ||
| 19 | Data::Quantity::Finance::Currency::USD->symbol(); | ||
| 20 | Data::Quantity::Finance::Currency::USD->fixed_decimal(); | ||
| 21 | |||
| 22 | # $n_val = Data::Quantity::Finance::Currency::USD->numeric_value( $candidate_n_val ); | ||
| 23 | sub numeric_value { | ||
| 24 | my $class_or_item = shift; | ||
| 25 | my $n_val = shift; | ||
| 26 | |||
| 27 | my $symbol = $class_or_item->symbol; | ||
| 28 | my $fixed_decimal = $class_or_item->fixed_decimal; | ||
| 29 | |||
| 30 | if ( $n_val =~ /^\-?\d+$/ ) { | ||
| 31 | return $n_val; | ||
| 32 | } | ||
| 33 | |||
| 34 | $n_val =~ s/\Q$symbol\E//; | ||
| 35 | |||
| 36 | $n_val = Data::Quantity::Number::Number->numeric_value( $n_val ); | ||
| 37 | return unless ( defined $n_val ); | ||
| 38 | |||
| 39 | return $n_val * ( 10 ** $fixed_decimal ); | ||
| 40 | } | ||
| 41 | |||
| 42 | # $string = $quantity->readable_value($n_val); | ||
| 43 | # $string = Data::Quantity::Finance::Currency::USD->readable_value($n_val); | ||
| 44 | sub readable_value { | ||
| 45 | my $class_or_item = shift; | ||
| 46 | my $n_val = shift; | ||
| 47 | |||
| 48 | return unless ( $n_val =~ /\d/ ); | ||
| 49 | |||
| 50 | my $symbol = $class_or_item->symbol; | ||
| 51 | my $fixed_decimal = $class_or_item->fixed_decimal; | ||
| 52 | |||
| 53 | $n_val = $n_val / ( 10 ** $fixed_decimal ); | ||
| 54 | |||
| 55 | my $signer = '-' if $n_val < 0; | ||
| 56 | |||
| 57 | return $signer . $symbol . Data::Quantity::Number::Number->readable_value( $n_val, $fixed_decimal ); | ||
| 58 | } | ||
| 59 | |||
| 60 | 1; |