Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to switch floating decimal number char from "." to "," ?

Reply
Thread Tools

How to switch floating decimal number char from "." to "," ?

 
 
Pete Sammet
Guest
Posts: n/a
 
      06-04-2010
As I found out by default Perl produces floating point number output as

123456.78

where ".78" is the fraction part of the number.

However in Europe another format is used:

123456,78

How exactly can I switch from the first to the second format?

I read a solution with

$myvar ~= tr/./,/;

but I don't want such a "afterwork" transformation.

The output should AUTOMATICALLY contain "," even during the calculation:

$num = 5/4;
print $num;

should show 1,25

Pete

 
Reply With Quote
 
 
 
 
Denis McMahon
Guest
Posts: n/a
 
      06-04-2010
On 04/06/10 11:15, Pete Sammet wrote:

> where ".78" is the fraction part of the number.


> However in Europe another format is used:


> 123456,78


First hit of google for "perl locale" looked relevant.

Rgds

Denis McMahon

 
Reply With Quote
 
 
 
 
Martijn Lievaart
Guest
Posts: n/a
 
      06-07-2010
On Sat, 05 Jun 2010 21:33:20 +0100, Ben Morrow wrote:

> This is a bad plan. Locales (specifically, the 'locale' pragma) and
> Unicode don't play nicely together in Perl, and if you're processing
> international text you will probably end up with Unicode strings. A


Can you expand on this? What exactly goes wrong (or is unexpected)?

M4

 
Reply With Quote
 
Martijn Lievaart
Guest
Posts: n/a
 
      06-07-2010
On Mon, 07 Jun 2010 17:21:07 +0100, Ben Morrow wrote:

> Quoth Martijn Lievaart <(E-Mail Removed)>:
>> On Sat, 05 Jun 2010 21:33:20 +0100, Ben Morrow wrote:
>>
>> > This is a bad plan. Locales (specifically, the 'locale' pragma) and
>> > Unicode don't play nicely together in Perl, and if you're processing
>> > international text you will probably end up with Unicode strings. A

>>
>> Can you expand on this? What exactly goes wrong (or is unexpected)?

>

(snip)
>
> Confused yet?


That's just plain buggy I would say, or is there some logic I don't see?

Besides, your examples did not work for me completely, to get the same
regex matching I had to set LANG as well.

M4
 
Reply With Quote
 
Martijn Lievaart
Guest
Posts: n/a
 
      06-07-2010
On Mon, 07 Jun 2010 18:27:08 +0100, Ben Morrow wrote:

>> That's just plain buggy I would say, or is there some logic I don't
>> see?

>
> No, it's just plain buggy. The bugs have been there since 5.8.0, they
> are well known, and the only reason they haven't been fixed yet is
> because it's extremely difficult (both to work out what the behaviour
> *should* be, and to write the actual code). The problem *is* currently
> being worked on (mostly by Karl Williamson), but don't hold your breath.


Thx for the info.

/me writes down after "never use threads in Perl", "never use locales in
Perl".

I get the problem, but it does suck.

>
>> Besides, your examples did not work for me completely, to get the same
>> regex matching I had to set LANG as well.

>
> You probably had LANG set in your environment already, which I don't.
> IIRC LANG overrides LC_ALL.
>


Ah, that explains it.

M4

 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      06-07-2010
On 2010-06-07 17:00, Martijn Lievaart <(E-Mail Removed)> wrote:
> On Mon, 07 Jun 2010 17:21:07 +0100, Ben Morrow wrote:
>> Quoth Martijn Lievaart <(E-Mail Removed)>:
>>> On Sat, 05 Jun 2010 21:33:20 +0100, Ben Morrow wrote:
>>>
>>> > This is a bad plan. Locales (specifically, the 'locale' pragma) and
>>> > Unicode don't play nicely together in Perl, and if you're processing
>>> > international text you will probably end up with Unicode strings. A
>>>
>>> Can you expand on this? What exactly goes wrong (or is unexpected)?

>>

> (snip)
>>
>> Confused yet?

>
> That's just plain buggy I would say, or is there some logic I don't see?


The logic is that if you use locale then byte strings are supposed to be
encoded according to the current locale. This affects only regexps and
string comparisons according to perldoc locale. It could be argued that
it should also affect implicit upgrading to character strings.

> Besides, your examples did not work for me completely, to get the same
> regex matching I had to set LANG as well.


That looks like a bug. LC_ALL is supposed to override LANG.

hp

 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Re: String to char and decimal number conversion Chris Rebert Python 2 01-11-2011 10:41 AM
Finding multiple of a decimal number in a floating point list Alejandro Python 2 08-18-2008 06:10 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM



Advertisments