Re: Calculations with formatted numbers
by mpeters (Chaplain) on Feb 15, 2005 at 15:47 UTC
|
Number::Format looking specifically at format_number(), THOUSANDS_SEP and DECIMAL_POINT. | [reply] [d/l] [select] |
|
|
A starting point. At least i would get rid of all those s/\.//; in my code, because of the unformat()-method.
But itīs still a bit ugly. I want some magic ,)
| [reply] |
A reply falls below the community's threshold of quality. You may see it by logging in. |
Re: Calculations with formatted numbers
by fglock (Vicar) on Feb 15, 2005 at 17:47 UTC
|
package Number::Formatted;
use Number::Format;
my $format = new Number::Format (
thousands_sep => '.',
decimal_point => ',' );
use overload
'""' => sub { $format->format_number( ${$_[0]} ) },
'+' => sub {
my $num = ref( $_[1] ) ? ${$_[1]} : $_[1];
$num = ${$_[0]} + $num;
return bless \$num, Number::Formatted;
},
fallback => 1;
sub new {
my $num = $format->unformat_number( $_[1] );
return bless \$num, $_[0];
}
package main;
my $n = new Number::Formatted ( '1.234,56' );
my $m = new Number::Formatted ( '123,45' );
print $n, "\n";
print $n + 10, "\n";
print $n + $m, "\n";
# 1.234,56
# 1.244,56
# 1.358,01
| [reply] [d/l] |
|
|
Thank you! Exactly what i was looking for!
So i created similar overloads for the other operators (-, *, /, **) that work fine.
But these do not:
#...
'++' => sub
{
$num = ${$_[0]} + 1;
return bless \$num, Number::Formatted;
},
'--' => sub
{
$num = ${$_[0]} - 1;
return bless \$num, Number::Formatted;
},
Any idea why?
| [reply] [d/l] |
|
|
package Number::Formatted;
use Number::Format;
my $format = new Number::Format (
thousands_sep => '.',
decimal_point => ',' );
use overload
'""' => sub { $format->format_number( ${$_[0]} ) },
'+' => sub {
my $num = ref( $_[1] ) ? ${$_[1]} : $_[1];
$num = ${$_[0]} + $num;
$num = - $num if $_[2];
return bless \$num, Number::Formatted;
},
'-' => sub {
my $num = ref( $_[1] ) ? ${$_[1]} : $_[1];
$num = ${$_[0]} - $num;
$num = - $num if $_[2];
return bless \$num, Number::Formatted;
},
'++' => sub { ++ ${$_[0]}; shift },
'--' => sub { -- ${$_[0]}; shift },
;
sub new {
my $num = $format->unformat_number( $_[1] );
return bless \$num, $_[0];
}
package main;
my $n = new Number::Formatted ( '1.234,56' );
my $m = new Number::Formatted ( '123,45' );
print $n, "\n";
print $n + 10, "\n";
print $n + $m, "\n";
print $n - $m, "\n";
print ++$n, "\n";
# 1.234,56
# 1.244,56
# 1.358,01
# 1.111,11
# 1.235,56
| [reply] [d/l] |
Re: Calculations with formatted numbers
by jcoxen (Deacon) on Feb 15, 2005 at 15:49 UTC
|
Do you really need to do the math with formatted numbers? Can't you do the calculations you need and then just format the numbers before outputting them?
And also, why are you reversing the accepted use (at least here in the US) of the dot as the decimal-seperator and the comma as the thousand block seperator?
Jack
Update Mea Culpa. Sorry if I offended anyone but I was asking questions - seeking information. I am aware that standards are different outside the US - not better, not worse, just different - eg paper sizes, metric vs English measurements, Centigrade vs Farenheit, etc. Quite frankly, that particular difference had never come up in water cooler discussions with my European co-workers. | [reply] |
|
|
Because the German notation has the "," (comma) as the decimal separator, and the "." (dot) as the thousands separator. And decent software supports it, by respecting the Windows locale settings (for example Excel), while badly written software does badly, for example, when reading in data (like Excel does, too).
| [reply] |
|
|
jcoxen, welcome to Europe.
_____________________________________________________
Jeff japhy Pinyan,
P.L., P.M., P.O.D, X.S.:
Perl,
regex,
and perl
hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
| [reply] |
|
|
In the EU the comma is used as decimal point and the dot as grouping opreator... (not really logic but I didn't invent the rules)
Which is different from the US-usage...
Update: I wasted too long time with posting this post... I checked if anyone posted, I clicked preview, create, and then all of a sudden two people had posted :(
| [reply] |
|
|
In the EU the comma is used as decimal point and the dot as grouping opreator..
That is not exactly true. The EU is a political entity which has come about in the past few decades, and it does not have any impact on how people write numbers (at least, not yet; now you mention it, an EU number-harmonization directive is exactly their kind of thing).
There are many countries in Europe which have not signed up to being part of the EU but which use a comma as a decimal mark and dot for thousands separator. And the UK and the Republic of Ireland are both members of the EU but use dot and comma the same way as in the USA.
To help avoid confusion between the different cultural practices of using commas and dots in numbers, the SI and ISO standards say that neither should ever be used as a thousands separator, which should instead be a thin space. The trouble is that it's very hard to persuade a computer to produce such a thin space (especially in a fixed-width terminal window!). More details on this page, Attack of the Thousands Separator.
Smylers
| [reply] |
|
|
| [reply] |