| 1 | package oEdtk::FPField; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base 'oEdtk::Field'; | ||
| 7 | our $VERSION = 2.1126; | ||
| 8 | |||
| 9 | sub new { | ||
| 10 | my ( $class, $name, $ilen, $flen ) = @_; | ||
| 11 | |||
| 12 | $flen = 0 if ( !( defined $flen ) ); | ||
| 13 | my $self = $class->SUPER::new( $name, $ilen + $flen ); | ||
| 14 | $self->{'intlen'} = $ilen; | ||
| 15 | $self->{'fraclen'} = $flen; | ||
| 16 | return $self; | ||
| 17 | } | ||
| 18 | |||
| 19 | sub process { | ||
| 20 | my ( $self, $data ) = @_; | ||
| 21 | |||
| 22 | $data =~ s/\s+//g; | ||
| 23 | my $flen = $self->{'fraclen'}; | ||
| 24 | return $data if $data eq ''; | ||
| 25 | |||
| 26 | if ( $data !~ /\./ ) { | ||
| 27 | $data /= 10**$flen; | ||
| 28 | } | ||
| 29 | return sprintf( "%.${flen}f", $data ); | ||
| 30 | } | ||
| 31 | |||
| 32 | 1; | ||
| 33 | |||
| 34 | =head1 NAME | ||
| 35 | |||
| 36 | oEdtk::FPField - fixed-point numeric field | ||
| 37 | |||
| 38 | =head1 SYNOPSIS | ||
| 39 | |||
| 40 | use oEdtk::FPField; | ||
| 41 | |||
| 42 | my $field = oEdtk::FPField->new('MONTANT', 8, 2); | ||
| 43 | print $field->process('00012345'), "\n"; # '123.45' | ||
| 44 | |||
| 45 | =head1 DESCRIPTION | ||
| 46 | |||
| 47 | An C<oEdtk::Field> subclass for fixed-point numbers. The field is described by | ||
| 48 | an integer length and a fractional length, and C<process> renders the value | ||
| 49 | with exactly the requested number of decimals. | ||
| 50 | |||
| 51 | =head1 METHODS | ||
| 52 | |||
| 53 | =head2 new | ||
| 54 | |||
| 55 | my $field = oEdtk::FPField->new($name, $ilen, $flen); | ||
| 56 | |||
| 57 | Construct a field whose byte length is C<$ilen + $flen>. C<$flen> defaults to | ||
| 58 | C<0>. | ||
| 59 | |||
| 60 | =head2 process | ||
| 61 | |||
| 62 | my $value = $field->process($data); | ||
| 63 | |||
| 64 | Strip the blanks from C<$data>; an empty value returns C<''>. A value without a | ||
| 65 | decimal point is scaled down by C<10**$flen>, then formatted with C<$flen> | ||
| 66 | decimals. | ||
| 67 | |||
| 68 | =head1 AUTHOR | ||
| 69 | |||
| 70 | The oEdtk team. | ||
| 71 | |||
| 72 | =cut |