# Copyright (c) 1999,2000 bivio Software, Inc. All rights reserved. # # Visit http://www.bivio.biz for more info. # # This library is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; If not, you may get a copy from: # http://www.opensource.org/licenses/lgpl-license.html # # $Id: Amount.pm,v 2.2 2006/10/16 21:08:05 moeller Exp $ package Bivio::UI::HTML::Format::Amount; use strict; $Bivio::UI::HTML::Format::Amount::VERSION = sprintf('%d.%02d', q$Revision: 2.2 $ =~ /\d+/g); $_ = $Bivio::UI::HTML::Format::Amount::VERSION; =head1 NAME Bivio::UI::HTML::Format::Amount - formats numeric values =head1 RELEASE SCOPE bOP =head1 SYNOPSIS use Bivio::UI::HTML::Format::Amount; Bivio::UI::HTML::Format::Amount->new(); =cut =head1 EXTENDS L =cut use Bivio::UI::HTML::Format; @Bivio::UI::HTML::Format::Amount::ISA = ('Bivio::UI::HTML::Format'); =head1 DESCRIPTION C formats a numeric value to a specified number of decimal points. =cut #=IMPORTS use Bivio::TypeError; use Bivio::Type::Amount; #=VARIABLES =head1 METHODS =cut =for html =head2 static get_widget_value(string amount) : string =head2 static get_widget_value(string amount, int round, boolean want_parens, boolean zero_as_blank) : string Formats a numeric amount to the specified number of decimal digits. L is used to check whether the amount is a valid number. Default I is two (2). Returns the empty string if I is not defined. If I, negative numbers will be displayed with parenthesis and positive numbers will be bracketed in spaces. if I, 0 will be rendered as ' '. =cut sub get_widget_value { my(undef, $amount, $round, $want_parens, $zero_as_blank) = @_; return '' unless defined($amount); return ' ' if $zero_as_blank && $amount == 0; $round = 2 unless defined($round); $amount = Bivio::Type::Amount->round($amount, $round); # check for leading '-' and not '-0.00' my($negative) = $amount =~ /^[-]/ && $amount =~ /[^\-^0^\.]/; my($num, $dec); if (($num, $dec) = $amount =~ /^[+-]?(.*)\.(.*)$/) { ; } else { $num = $amount; $num =~ s/^[+-]//; } # put ',' in the number if (length($num) > 3) { my(@chars) = reverse(split(//, $num)); $num = ''; for (my($i) = 0; $i < int(@chars); $i++) { $num = ','.$num unless ($i == 0 || $i % 3); $num = $chars[$i].$num; } } my($result) = ($round > 0 && defined($dec)) ? ($num.'.'.$dec) : $num; return ' ' if $zero_as_blank && $result =~ /^0(\.0+)?$/; #TODO: really want   around positive values. Add result_is_html() return $negative ? '('.$result.')' : ' '.$result.' ' if $want_parens; return $negative ? '-'.$result : $result; } #=PRIVATE METHODS =head1 COPYRIGHT Copyright (c) 1999,2000 bivio Software, Inc. All rights reserved. Visit http://www.bivio.biz for more info. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; If not, you may get a copy from: http://www.opensource.org/licenses/lgpl-license.html =head1 VERSION $Id: Amount.pm,v 2.2 2006/10/16 21:08:05 moeller Exp $ =cut 1;