Skip Menu |
 

This queue is for tickets about the Number-Format CPAN distribution.

Report information
The Basics
Id: 124862
Status: new
Priority: 0/
Queue: Number-Format

People
Owner: Nobody in particular
Requestors: smylers [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: format_price ignores NEG_FORMAT
Download (untitled) / with headers
text/plain 2.1k
format_price doesn't take NEG_FORMAT into account when formatting negative numbers, unlike format_number. For instance, if trying to use parens to denote negative prices in the accounting style, format_price just uses the locale's negative number style, regardless of NEG_FORMAT: $ perl -MNumber::Format -wE '$f = Number::Format->new(neg_format => q[(x)]); say $f->format_number(-123); say $f->format_price(-123, undef, q[$])' (123) -$123.00 The docs for NEG_FORMAT claim that format_price uses format_negative, to apply NEG_FORMAT, to negative numbers: NEG_FORMAT is only used by format_negative() and is a string containing the letter 'x', where that letter will be replaced by a positive representation of the number being passed to that function. format_number() and format_price() utilize this feature by calling format_negative() if the number was less than 0. Unfortunately that does't seem to be the case. Actually format_price always invokes format_number on the absolute number, then uses its own logic to add the sign in the correct place relative to the currency symbol, based on locale settings, with n_sign_posn == 0 being the trigger for parens around negative numbers. And it turns out all locale parameters can be supplied as constructor arguments. So parens around negative prices can be achieved with: $ perl -MNumber::Format -wE '$f = Number::Format->new(n_sign_posn => 0); say $f->format_number(-123); say $f->format_price(-123, undef, q[$])' -123 ($123.00) But I couldn't see n_sign_posn documented anywhere as a valid argument to pass to ->new. And even if this is supported, it's an obscure way of specifying the desired behaviour, completely unintuitive to somebody reading the code. Also, see that n_sign_posn only affects format_price and not format_number. To have a formatter object that uses parens for both numbers and prices you have to set both neg_format and n_sign_pos: $ perl -MNumber::Format -wE '$f = Number::Format->new(neg_format => q[(x)], n_sign_posn => 0); say $f->format_number(-123); say $f->format_price(-123, undef, q[$])' (123) ($123.00) Please can we have a way of specifying accounting-style negative numbers that works for both numbers and prices? Thanks.


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.