1use strict;
2
3package HTML::FormFu::Deflator::FormatNumber;
4$HTML::FormFu::Deflator::FormatNumber::VERSION = '2.07';
5# ABSTRACT: Format a number for a locale
6
7use Moose;
8use MooseX::Attribute::Chained;
9extends 'HTML::FormFu::Deflator';
10
11use Number::Format;
12use POSIX qw( setlocale LC_NUMERIC );
13
14has precision => (
15    is      => 'rw',
16    default => 2,
17    lazy    => 1,
18    isa     => 'Int',
19    traits  => ['Chained'],
20);
21
22has trailing_zeroes => (
23    is      => 'rw',
24    default => 0,
25    lazy    => 1,
26    isa     => 'Int',
27    traits  => ['Chained'],
28);
29
30sub deflator {
31    my ( $self, $value ) = @_;
32
33    my $backup_locale = setlocale(LC_NUMERIC);
34
35    if ( my $locale = $self->locale ) {
36
37        # throwing errors from deflator() isn't supported
38        # if unable to set locale, return the original value
39
40        setlocale( LC_NUMERIC, $locale )
41            or return $value;
42    }
43
44    my $format = Number::Format->new;
45
46    $value = $format->format_number( $value, $self->precision,
47        $self->trailing_zeroes );
48
49    # restore locale
50    setlocale( LC_NUMERIC, $backup_locale );
51
52    return $value;
53}
54
55__PACKAGE__->meta->make_immutable;
56
571;
58
59__END__
60
61=pod
62
63=encoding UTF-8
64
65=head1 NAME
66
67HTML::FormFu::Deflator::FormatNumber - Format a number for a locale
68
69=head1 VERSION
70
71version 2.07
72
73=head1 SYNOPSIS
74
75    locale: de_DE
76    elements:
77      - type: Text
78        name: number
79        deflators:
80          - type: FormatNumber
81            precision: 4
82
83      - type: Text
84        name: price
85        deflators:
86          - type: FormatNumber
87            precision: 2
88            trailing_zeroes: 1
89
90    # "123456.22" will be rendered to "123.456,22" (german locale)
91
92=head2 locale
93
94If no locale is found, the server's locale will be used.
95
96This method is a special 'inherited accessor', which means it can be set on
97the form, a enclosing block element, the field, or this filter.
98When the value is read, if no value is defined it automatically traverses
99the element's hierarchy of parents, through any block elements and up to the
100form, searching for a defined value.
101
102=head1 AUTHOR
103
104Moritz Onken, C<onken at houseofdesign.de>
105
106=head1 SEE ALSO
107
108Is a sub-class of, and inherits methods from L<HTML::FormFu::Deflator>
109
110L<HTML::FormFu::Filter::FormatNumber>
111
112L<HTML::FormFu>
113
114=head1 COPYRIGHT & LICENSE
115
116Copyright 2008 Moritz Onken, all rights reserved.
117
118This program is free software; you can redistribute it and/or modify it
119under the same terms as Perl itself.
120
121=head1 AUTHOR
122
123Carl Franks <cpan@fireartist.com>
124
125=head1 COPYRIGHT AND LICENSE
126
127This software is copyright (c) 2018 by Carl Franks.
128
129This is free software; you can redistribute it and/or modify it under
130the same terms as the Perl 5 programming language system itself.
131
132=cut
133