Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > locale and print()

Reply
Thread Tools

locale and print()

 
 
Nico Rittner
Guest
Posts: n/a
 
      10-11-2004
Hi,

i am really confused about how perl locale
deals with print() ( not printf).
See the tiny example below.
If the var- Declaration is quoted, print()
behaves like expected: printf with comma,
print with dot .. unquoted both with comma.

If arithmetic operations are done with it,
"print $val + 7" outputs the result with
a comma in it. "print $val" itself outputs
a dot like expected. What is the difference
of perl's interpretions of quoted (string)
and unqoted decimal values. And how the
hell do arithmetic operations on $val
influence print() 's behaviour ??
Is it really true that print() will NOT
use locale definitions?

I have to do some kind of mathematical
calculations, show some results (formatted)
on screen and put the results in a mysql-table,
which only expects dots as decimal seperators,
i believe.
Doing tr/,/./ or disabling locale
before writing to mysql really can't
be the right way to do it, i think.

Tanks a lot
and regards,

Nico


<snippet>
use strict;
use POSIX qw(locale_h);
use locale; # also tried without this line.
setlocale(LC_NUMERIC,"de_DE@EURO");

my $val;

$val = "6.02";
printf ("%.2f\n",$val);
print $val;
print "\n";
print $val + 7;
print "\n\n";

$val = 6.02;
printf ("%.2f\n",$val);
print $val;
print "\n";
print $val + 7;
print "\n";
</snippet>

Output:
6,02
6.02
13,02

6,02
6,02
13,02
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      10-12-2004
Nico Rittner <> wrote:

> i am really confused about how perl locale
> deals with print() ( not printf).
> See the tiny example below.
> If the var- Declaration is quoted, print()
> behaves like expected: printf with comma,
> print with dot .. unquoted both with comma.



ie. in the first set of prints, $val is a string.


> If arithmetic operations are done with it,
> "print $val + 7" outputs the result with
> a comma in it. "print $val" itself outputs
> a dot like expected. What is the difference
> of perl's interpretions of quoted (string)
> and unqoted decimal values. And how the
> hell do arithmetic operations on $val
> influence print() 's behaviour ??



When a number is "stringified", it follows the locale.

There is no need to convert number->string for the 1st "print $val;"
so the dot remains.

The 2nd "print $val;" needs to do the conversion, so it uses comma.


> Is it really true that print() will NOT
> use locale definitions?



It *is* using locale definitions.

Whenever it needs to convert a number into a string, it is using a comma,
just like it is supposed to.


><snippet>
> use strict;
> use POSIX qw(locale_h);
> use locale; # also tried without this line.
> setlocale(LC_NUMERIC,"de_DE@EURO");

^^^^^
^^^^^

Isn't strict complaining about that undeclared variable?

Is this your actual code?


> my $val;
>
> $val = "6.02";
> printf ("%.2f\n",$val);
> print $val;



It started life as a string, no need to convert a number to a string here.


> $val = 6.02;
> printf ("%.2f\n",$val);
> print $val;



It started life as a number, need to stringify it for output.


> Output:
> 6,02
> 6.02
> 13,02
>
> 6,02
> 6,02
> 13,02



--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
the relation between C++ locale and C locale zade C++ 1 03-05-2010 06:04 PM
Re: List of locale values for locale.setlocale() under Windows Gabriel Genellina Python 0 02-18-2009 12:00 AM
Create C++ std::locale without changing C locale dertopper@web.de C++ 4 08-26-2008 01:15 PM
i18n problem, involving Locale.getDisplayLanguage and Locale.getDisplayCountry Maurice Hulsman Java 1 07-25-2004 06:11 PM
locale.nl_langinfo(RADIXCHAR) vs locale.localeconv()['decimal_point'] Jeff Epler Python 2 08-31-2003 02:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57