in reply to Thousands separator - this is getting annoying

You've almost diagnosed the problem correctly, but not quite. When Perl turns the string "1,000" into a number, it does not think it is reading a list. What happens is that, since ',' is not a valid character in a number in Perl, the string-to-number conversion just stops when it gets to the comma. This would be the same as '1j000' or '1~000'.

You don't show where your numbers are coming from, but you can avoid this problem by removing the commas from your 'numbers' before you use them as numbers: $num =~ tr/,//d; Of course, it would be simpler to not put the commas there in the first place.

Replies are listed 'Best First'.
Re: Re: Thousands separator - this is getting annoying
by Anonymous Monk on Jan 16, 2001 at 23:07 UTC
    Thanks for the fast reply. Numbers are coming straight out of database in that form, so I don't get much choice in the matter! Will just regexp them as you suggest.
    Cheers
    Martin GK
      Why do who have a number field as a string in the db in the first place? You'll lose lots of db-side operations on numbers in this form.
      AgentM Systems nor Nasca Enterprises nor Bone::Easy nor Macperl is responsible for the comments made by AgentM. Remember, you can build any logical system with NOR.
      That doesn't make any sense, unless, as agentM suggest, your number field is represented internally as a string. While you might have some unusual reason for doing so, it's not a good idea.

      If possible, I'd put the burden of conversion on the database. Depending on what you're using, and how you're querying it, you might be able to use

      SELECT INT(field) FROM table

      -or-

      SELECT CONVERT(NUMERIC (10,2),field) FROM table

      good luck!

        That doesn't make any sense, unless, as agentM suggest, your number field is represented internally as a string.

        Actually, it's likely that the database is storing the numbers as numbers, but that the format used when a number is retrieved from the database specifies commas. For example, in Oracle SQL*PLUS:

        14:23:48 FOCX> desc mytable Name Null? Type ----------------------------------------- -------- ---------------- ID NUMBER(7) 14:23:56 DB> select id from mytable; Press <enter> to continue... ID ------- 1000 1001 14:23:59 DB> set numformat 999,990 14:24:06 DB> / Press <enter> to continue... ID -------- 1,000 1,001