Calculations with ',' as decimal delimiter

M

Martin Kissner

hello together,

Is it possible to use coma (,) as decimal delimiter for some
calculations.
I have a loop and want to get rid of the substitution:

while($condition) {
$value =~ s/,/./;
$sum+=$value;
}

$value contains values like '1,05', '2,35' and so on.
$sum is supposed to contain the sum also with a ',' as decimal
delimiter.

Any sugestions will be appreciated.

Best regards
Martin
 
A

Anno Siegel

Martin Kissner said:
hello together,

Is it possible to use coma (,) as decimal delimiter for some
calculations.
I have a loop and want to get rid of the substitution:

while($condition) {
$value =~ s/,/./;
$sum+=$value;
}

$value contains values like '1,05', '2,35' and so on.
$sum is supposed to contain the sum also with a ',' as decimal
delimiter.

Numbers are numbers. They don't have a specific decimal separator.
That is (like the base) entirely a matter of a string representation.

If your system supports locale, you can use that:

use locale;
use POSIX qw( strtod);
POSIX::setlocale( POSIX::LC_NUMERIC(), 'de_DE');

print strtod( '12,34') + strtod( '56,78'), "\n";

perldoc perllocale, perldoc locale, perldoc POSIX for details.

Anno
 
M

Martin Kissner

Anno Siegel wrote :
If your system supports locale, you can use that:

use locale;
use POSIX qw( strtod);
POSIX::setlocale( POSIX::LC_NUMERIC(), 'de_DE');

print strtod( '12,34') + strtod( '56,78'), "\n";

Thank you.
This works perfectly.
But it looks like more "work" than the replacement I used before.

One more question:
I changed
$value =~ s/,/./
# calculation
$value =~ s/\./,/
top
$value =~ tr/,/./
# calculation
$value =~ tr/./,/

Am I right that the second is more efficient?

Best regards
Martin
 
A

Anno Siegel

Martin Kissner said:
Anno Siegel wrote :


Thank you.
This works perfectly.
But it looks like more "work" than the replacement I used before.

One more question:
I changed
$value =~ s/,/./
# calculation
$value =~ s/\./,/
top
$value =~ tr/,/./
# calculation
$value =~ tr/./,/

Am I right that the second is more efficient?

In theory, yes. It won't make a difference in practice. I'd still
use tr/// because it's the simplest tool that does the job.

Anno
 
M

Martin Kissner

Anno Siegel wrote :
In theory, yes. It won't make a difference in practice. I'd still
use tr/// because it's the simplest tool that does the job.

Okay, Thank you.

Best regards
Martin
 
P

Peter J. Holzer

Anno said:
If your system supports locale, you can use that:

use locale;
use POSIX qw( strtod);
POSIX::setlocale( POSIX::LC_NUMERIC(), 'de_DE');

print strtod( '12,34') + strtod( '56,78'), "\n";

Be careful there. This breaks silently if the locale 'de_DE' isn't
actually installed. Then you will just get the result 68 instead of
69,12. Be sure to check the return code of setlocale.

hp
 
B

Bart Lateur

Martin said:
Is it possible to use coma (,) as decimal delimiter for some
calculations.
I have a loop and want to get rid of the substitution:

while($condition) {
$value =~ s/,/./;
$sum+=$value;
}

$value contains values like '1,05', '2,35' and so on.

Why do you want to get rid of the substitution? It's a simple statement
and it takes just a fraction of a microsecond to execute. Any other
solution you can come up with will probably be longer to write down, and
take longer to execute.

Alternatively you can use

tr/,/./

which replaces all commas in the string.
 
M

Martin Kißner

Bart Lateur wrote :
Why do you want to get rid of the substitution? It's a simple statement
and it takes just a fraction of a microsecond to execute. Any other
solution you can come up with will probably be longer to write down, and
take longer to execute.

Alternatively you can use

tr/,/./

which replaces all commas in the string.

Yes, this is what I use now.

Best regards
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,973
Members
47,529
Latest member
JaclynShum

Latest Threads

Top