Bivio::UI::HTML::Format::Amount
# Copyright (c) 1999,2000 bivio Software, Inc. All rights reserved.
# $Id$
package Bivio::UI::HTML::Format::Amount;
use strict;
# NAME
#
# Bivio::UI::HTML::Format::Amount - formats numeric values
#
# RELEASE SCOPE
#
# bOP
#
# SYNOPSIS
#
# use Bivio::UI::HTML::Format::Amount;
# Bivio::UI::HTML::Format::Amount->new();
#
# EXTENDS
#
# Bivio::UI::HTML::Format
#
use Bivio::UI::HTML::Format;
@Bivio::UI::HTML::Format::Amount::ISA = ('Bivio::UI::HTML::Format');
# DESCRIPTION
#
#
Bivio::UI::HTML::Format::Amount formats a numeric value to a specified
# number of decimal points.
#
#=IMPORTS
use Bivio::TypeError;
use Bivio::Type::Amount;
#=VARIABLES
# METHODS
#
#
# static get_widget_value(string amount) : string
#
# 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.
# Bivio::Type::Number is used to check whether
# the amount is a valid number.
#
# Default round is two (2).
#
# Returns the empty string if amount is not defined.
#
# If want_parens, negative numbers will be displayed with parenthesis
# and positive numbers will be bracketed in spaces.
#
# if zero_as_blank, 0 will be rendered as ' '.
#
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
# COPYRIGHT
#
# Copyright (c) 1999,2000 bivio Software, Inc. All rights reserved.
#
# VERSION
#
# $Id$
#
1;