+ 15
🔢💻 Challenge: Putting commas! 💻🔢
Write a program that gets a number from user, and prints the number with commas inserted in the correct position. For example: * 123 --> 123 * 1234 --> 1,234 * 98765 --> 98,765 * 1223334444 --> 1,223,334,444 Bonus: Get your program to handle correctly negative numbers. All languages and paradigms are very welcome! One liners, two-liners, tri-liners, tetra-liners, poli-liners, multi-liners, hyper-liners, time-liners, etc, all welcome! Be creative, and most of all: Have fun!! Thanks for your attention.
18 Answers
+ 10
Shortest one in Java?
https://code.sololearn.com/c4aFRHFAsB7U/?ref=app
+ 8
https://code.sololearn.com/cfomdHt1yWua/?ref=app
+ 7
https://code.sololearn.com/cqE6z6DVpz95/?ref=app
In one line
+ 7
@ʎʇɹǝʍb But your link to a C# program. 😂
+ 7
Here's my C# implementation! ✌
Non-LINQ One-Liner〰
new List<int>
{
123,
-123,
1234,
-1234,
98765,
-98765,
1223334444
}.ForEach(n => Console.Write(n.ToString("n0")));
It's a trivial challenge for C#. Enjoy~ ❤
https://code.sololearn.com/cpZcRsQX4Bmk/?ref=app
+ 5
@qwerty: Wow! that was fast! 😊
+ 5
Number.prototype.commas = function (){
return this.toString().split('').map(function(n,i,a) {
return (a[0]=='-'&&i==1)||(i==0)||(a.length-i)%3!=0? n:','+n;
}).join('');
}
https://code.sololearn.com/WG3v612Z4gKl/?ref=app
+ 4
Here you go. Checks for valid input
https://code.sololearn.com/cCYpRcd5E2IT/?ref=app
+ 3
https://code.sololearn.com/cY7cIYWwteKj/?ref=app
+ 3
+ 2
My example on cpp :)
https://code.sololearn.com/cOzYL6Rqqa4G/?ref=app
+ 2
Here is my answer. I know my code would be better if I could use regexp efficientlly...
https://code.sololearn.com/c1K4lJApSdSD/?ref=app
+ 2
Here is my code. Can handle leading zeros.
https://code.sololearn.com/WT9WBqm7EhED/?ref=app