Given a number, say, -2982342342363.23425785 convert it to -2,982,342,342,363.23425785.
Extra points for not using look-behind
Extra points for not using regular expressions.
|
Given a number, say,
|
||||
| show 2 more comments |
Java, 50 chars
Works as specified in most locales. Does the correct thing in all locales :) Edit: If you really want commas no matter the default locale, do:
which is 70 characters (60 if you've already imported |
|||||||||
|
Spreadsheet,
|
|
Scala:
144 chars without 1st and last line, but this isn't code golf, is it?. Imho no look behind and no regex used. Result:
|
|||||
|
Perl, regular expressionUsing a regular expression and no look-behinds but look-aheads:
Each digit is replaced by itself plus a comma if it is followed by n digits where n is a multiple of three and immedetiately followed by a period or the end of the string. |
|||
|
|
Haskell, 105
This doesn't use regular expressions (obviously), but it does backtrack (namely, it reverses digits before adding separators). |
|||
|
|
Ruby 111 chars
|
|||
|
|
PHPUsing the built in function for formatting numbers:
Doing it by hand:
|
|||
|
vi, 25 chars
The script moves the cursor to the last digit of the number, then tries to search backwards for a decimal point. From there (either the last digit, or the decimal point if there was one), the following process is executed 99 times: the cursor moves left 3 times, then attempts to move left again. If it was able to move left, then a comma is inserted; otherwise, the process aborts. 9 times is probably sufficient instead of 99, so this could be reduced to EDIT: I realized that I wasn't accounting for possible edge cases with a negative sign in front, so I added six characters to deal with it. At the start, 'eb' will effectively move the cursor to the first digit of the number. 'd0' will delete everything before that (which is either the negative sign or nothing). Then at the end, '0' moves to the start of the line and 'P' inserts whatever was deleted earlier. |
||||
|
|
Python2 - 219
or I can do this... Python2 - 112
Personally I like the first one more... mainly because of the map instead of logic hack. The second one is verbatim from StackOverflow of all places. |
|||
|
|
Python 2.7, 127/122 bytestaking input from stndin:
as a function:
could probably be golfed more... |
|||
|
|
|
In Lua:
Odd mix of regular expressions and arithmetic... It allows to handle non-numbers, sign, etc. in a quite concise way. I need the two steps RE as they are quite limited in Lua. [EDIT] I don't really comply to the requirement as I made an array of strings instead of an array of numbers. On the other hand, by default Lua is compiled with 32bit floats, not 64bit doubles, so the given number is truncated to the first digit after the decimal point... So Lua remains out of the competition. Oh well, it was fun to play with Lua again. |
||||
|
|
This is in coffee script. There's no look behind in JavaScript, but you can pass a function to edit: changed to work in webkit browsers. |
||||
|
|
Python 3: 100 or 102 characters100 characters as a function:
102 reading from stdin:
|
|||
|
|
|
Python 2.7: 29 charactersA bit late to the party, but I just read about this new feature in 2.7 and figured this still deserves to be here:
Used by calling |
||||
|
|
PHP, 56No Regex, no look-behind.
My first attempts tried calculating how many decimals to keep in order to use it as the optional second parameter for My second attempt treated the number as a string, splitting on Then I realized I could just let A different version, not using
Longer and a bit more readable:
|
|||||||
|
C (99)No regexes, no backtracking, not using built-in formatting of real numbers.
|
|||
|
|
Q, 27 chars (integer input)
q){(|:)","sv 3 cut (|:) -3!x}[23498723] "23,498,723" Q, 48 chars (float input)
q){,;".",a@1","sv 3 cut(|:)(*:)a:"." vs -3!x}[234554.21434] "234,554.21434" |
||||
|
|
Some JavaScript approaches: (1) 74 characters but fails on some inputs, recursive creation:
It has two failure channels: (1) if the substring contains subnumbers with leading 0's it sometimes will produce e.g.
(2) Split over lookahead and join with commas, 95 characters, one statement:
I can't see a better way to handle negatives in this case. (3) Array inserts every 3 characters to the left of the '.', 100 characters, uses a Bad Part of JS (
|
|||
|
|
Ruby (didn't golf or count. No regexps.)
|
|||
|
|
-2982,342,342,363.23425785and not-2,982,342,342,363.23425785? – Howard Jan 3 '12 at 8:40