Formatting integers with commas (12345 → 12,345) Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Separate numbers with commasFormat a number to include commasHaving fun with JNI: formatting a numberFormatting a list with commas and occasional line breaksArbitrary large unsigned integersF'up: Arbitrary large unsigned integersFormatting big integers as hexadecimal digits separated by colonsFormatting 3 integers (hours, mins, secs) to `00:00:00`?Function that adds commas between groups of 3 digits in a stringPrinting integers with spaces between the digits

How to down pick a chord with skipped strings?

How do I find out the mythology and history of my Fortress?

Is it a good idea to use CNN to classify 1D signal?

Is there any way for the UK Prime Minister to make a motion directly dependent on Government confidence?

What is the meaning of the simile “quick as silk”?

Can a new player join a group only when a new campaign starts?

Why wasn't DOSKEY integrated with command.com?

Why are there no cargo aircraft with "flying wing" design?

What is the probability distribution of linear formula?

Closed form of recurrent arithmetic series summation

What do you call the main part of a joke?

2001: A Space Odyssey's use of the song "Daisy Bell" (Bicycle Built for Two); life imitates art or vice-versa?

Is there such thing as an Availability Group failover trigger?

What would be the ideal power source for a cybernetic eye?

Why are the trig functions versine, haversine, exsecant, etc, rarely used in modern mathematics?

Ports Showing Closed/Filtered in Nmap Scans

Can melee weapons be used to deliver Contact Poisons?

Irreducible of finite Krull dimension implies quasi-compact?

Is the Standard Deduction better than Itemized when both are the same amount?

Is it fair for a professor to grade us on the possession of past papers?

How do pianists reach extremely loud dynamics?

Around usage results

Can an alien society believe that their star system is the universe?

How can I use the Python library networkx from Mathematica?



Formatting integers with commas (12345 → 12,345)



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Separate numbers with commasFormat a number to include commasHaving fun with JNI: formatting a numberFormatting a list with commas and occasional line breaksArbitrary large unsigned integersF'up: Arbitrary large unsigned integersFormatting big integers as hexadecimal digits separated by colonsFormatting 3 integers (hours, mins, secs) to `00:00:00`?Function that adds commas between groups of 3 digits in a stringPrinting integers with spaces between the digits



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








-2












$begingroup$


Beginner/occasional C tinkerer here. (Reasonably comfortable with scripting languages, tend to avoid C due to its terseness, but trying to work past that.)



I happened to want to insert commas into some integers. I got curious about various interesting (non-string-based) ways one might achieve that, found how to arithmetically reverse a string, and ended up writing this code.



#include <stdio.h>

int fmtn(unsigned long long n, char *buf, int maxlen)

int l = 0;
for (unsigned long long x = n; x; x /= 10, l++);

int l2 = l + (int)((l / 3.1));
if (l2 > maxlen) l2 = maxlen;
if (l > maxlen) l = maxlen;

int o = l2 - 1;
int c = l - 1;

for (; o > 0, n; c--, n /= 10)
if (o >= 0) buf[o--] = (n % 10) + 48;
if (o >= 0 && (l - c) % 3 == 0) buf[o--] = ',';


buf[l2] = '';

return l2;



#define test(x) do fmtn(x, b, 30); printf(""%s"n", b); while(0);
#define testn(x, n) do fmtn(x, b, n); printf(""%s"n", b); while(0);

int main(int argc, char **argv)

char b[30];

testn(123, 1);
testn(123, 2);
testn(1234, 3);
testn(1234, 4);
testn(1234, 5);

puts("");

test(123);
test(12345);
test(123456);
test(1234567);
test(12345678);
test(123456789);
test(1234567890);
test(12345678901);
test(123456789012);
test(1234567890123);
test(12345678901234);
test(123456789012345);
test(1234567890123456);
test(12345678901234567);
test(123456789012345678);
test(1234567890123456789);
test(12345678901234567890ull);





Compiling with -Wall produces this interesting message - I'm pretty sure that o will routinely <= 0:



In function 'fmtn':
15:14: warning: left-hand operand of comma expression has no effect [-Wunused-value]
for (; o > 0, n; c--, n /= 10) {
^


Here's what the supplied main outputs on my machine. The first part is a demonstration of what happens when maxlen (which doesn't count the '') is too short (no buffer overflows! yay!):



"3"
"23"
"234"
",234"
"1,234"

"123"
"12,345"
"123,456"
"1,234,567"
"12,345,678"
"123,456,789"
"1,234,567,890"
"12,345,678,901"
"123,456,789,012"
"1,234,567,890,123"
"12,345,678,901,234"
"123,456,789,012,345"
"1,234,567,890,123,456"
"12,345,678,901,234,567"
"123,456,789,012,345,678"
"1,234,567,890,123,456,789"
"12,345,678,901,234,567,890"


The part I'm mostly... fascinated? with is the magic 3.1 near the top which is calculating what the length of the output string with commas added will be (the code of course works backwards). 3.1 Seems To Work™ for the values I'm supplying, but I'm 100.0% sure it's probably secretly horribly broken on some large inputs (which I've not figured out how to test yet).



There are also probably a myriad of other issues I've not thought of in terms of coding semantics and style.










share|improve this question









New contributor




i336_ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$











  • $begingroup$
    The warning tells you, that your if condition does not work as you would expect. Try something like for (; o > 0 && n > 0; c--, n /= 10), then the warning will go away (see also this).
    $endgroup$
    – Alex
    6 hours ago











  • $begingroup$
    @Alex: OH. It's telling me , != &&, which I do indeed want here. (Compilers are cool; glad I didn't succumb to instinct and presume.) Thanks! Fixed. [Edit: Fixed in my version, that is; should I fix the version here too?]
    $endgroup$
    – i336_
    6 hours ago







  • 2




    $begingroup$
    If you're looking at the guidelines here, you will see that your question is off-topic until its properly working. So yes, fix your question, remove the paragraph about the warning and then your code is ready to be reviewed.
    $endgroup$
    – Alex
    5 hours ago










  • $begingroup$
    It would also be nice to explain a bit about how the code does what it does.
    $endgroup$
    – user673679
    3 hours ago

















-2












$begingroup$


Beginner/occasional C tinkerer here. (Reasonably comfortable with scripting languages, tend to avoid C due to its terseness, but trying to work past that.)



I happened to want to insert commas into some integers. I got curious about various interesting (non-string-based) ways one might achieve that, found how to arithmetically reverse a string, and ended up writing this code.



#include <stdio.h>

int fmtn(unsigned long long n, char *buf, int maxlen)

int l = 0;
for (unsigned long long x = n; x; x /= 10, l++);

int l2 = l + (int)((l / 3.1));
if (l2 > maxlen) l2 = maxlen;
if (l > maxlen) l = maxlen;

int o = l2 - 1;
int c = l - 1;

for (; o > 0, n; c--, n /= 10)
if (o >= 0) buf[o--] = (n % 10) + 48;
if (o >= 0 && (l - c) % 3 == 0) buf[o--] = ',';


buf[l2] = '';

return l2;



#define test(x) do fmtn(x, b, 30); printf(""%s"n", b); while(0);
#define testn(x, n) do fmtn(x, b, n); printf(""%s"n", b); while(0);

int main(int argc, char **argv)

char b[30];

testn(123, 1);
testn(123, 2);
testn(1234, 3);
testn(1234, 4);
testn(1234, 5);

puts("");

test(123);
test(12345);
test(123456);
test(1234567);
test(12345678);
test(123456789);
test(1234567890);
test(12345678901);
test(123456789012);
test(1234567890123);
test(12345678901234);
test(123456789012345);
test(1234567890123456);
test(12345678901234567);
test(123456789012345678);
test(1234567890123456789);
test(12345678901234567890ull);





Compiling with -Wall produces this interesting message - I'm pretty sure that o will routinely <= 0:



In function 'fmtn':
15:14: warning: left-hand operand of comma expression has no effect [-Wunused-value]
for (; o > 0, n; c--, n /= 10) {
^


Here's what the supplied main outputs on my machine. The first part is a demonstration of what happens when maxlen (which doesn't count the '') is too short (no buffer overflows! yay!):



"3"
"23"
"234"
",234"
"1,234"

"123"
"12,345"
"123,456"
"1,234,567"
"12,345,678"
"123,456,789"
"1,234,567,890"
"12,345,678,901"
"123,456,789,012"
"1,234,567,890,123"
"12,345,678,901,234"
"123,456,789,012,345"
"1,234,567,890,123,456"
"12,345,678,901,234,567"
"123,456,789,012,345,678"
"1,234,567,890,123,456,789"
"12,345,678,901,234,567,890"


The part I'm mostly... fascinated? with is the magic 3.1 near the top which is calculating what the length of the output string with commas added will be (the code of course works backwards). 3.1 Seems To Work™ for the values I'm supplying, but I'm 100.0% sure it's probably secretly horribly broken on some large inputs (which I've not figured out how to test yet).



There are also probably a myriad of other issues I've not thought of in terms of coding semantics and style.










share|improve this question









New contributor




i336_ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$











  • $begingroup$
    The warning tells you, that your if condition does not work as you would expect. Try something like for (; o > 0 && n > 0; c--, n /= 10), then the warning will go away (see also this).
    $endgroup$
    – Alex
    6 hours ago











  • $begingroup$
    @Alex: OH. It's telling me , != &&, which I do indeed want here. (Compilers are cool; glad I didn't succumb to instinct and presume.) Thanks! Fixed. [Edit: Fixed in my version, that is; should I fix the version here too?]
    $endgroup$
    – i336_
    6 hours ago







  • 2




    $begingroup$
    If you're looking at the guidelines here, you will see that your question is off-topic until its properly working. So yes, fix your question, remove the paragraph about the warning and then your code is ready to be reviewed.
    $endgroup$
    – Alex
    5 hours ago










  • $begingroup$
    It would also be nice to explain a bit about how the code does what it does.
    $endgroup$
    – user673679
    3 hours ago













-2












-2








-2





$begingroup$


Beginner/occasional C tinkerer here. (Reasonably comfortable with scripting languages, tend to avoid C due to its terseness, but trying to work past that.)



I happened to want to insert commas into some integers. I got curious about various interesting (non-string-based) ways one might achieve that, found how to arithmetically reverse a string, and ended up writing this code.



#include <stdio.h>

int fmtn(unsigned long long n, char *buf, int maxlen)

int l = 0;
for (unsigned long long x = n; x; x /= 10, l++);

int l2 = l + (int)((l / 3.1));
if (l2 > maxlen) l2 = maxlen;
if (l > maxlen) l = maxlen;

int o = l2 - 1;
int c = l - 1;

for (; o > 0, n; c--, n /= 10)
if (o >= 0) buf[o--] = (n % 10) + 48;
if (o >= 0 && (l - c) % 3 == 0) buf[o--] = ',';


buf[l2] = '';

return l2;



#define test(x) do fmtn(x, b, 30); printf(""%s"n", b); while(0);
#define testn(x, n) do fmtn(x, b, n); printf(""%s"n", b); while(0);

int main(int argc, char **argv)

char b[30];

testn(123, 1);
testn(123, 2);
testn(1234, 3);
testn(1234, 4);
testn(1234, 5);

puts("");

test(123);
test(12345);
test(123456);
test(1234567);
test(12345678);
test(123456789);
test(1234567890);
test(12345678901);
test(123456789012);
test(1234567890123);
test(12345678901234);
test(123456789012345);
test(1234567890123456);
test(12345678901234567);
test(123456789012345678);
test(1234567890123456789);
test(12345678901234567890ull);





Compiling with -Wall produces this interesting message - I'm pretty sure that o will routinely <= 0:



In function 'fmtn':
15:14: warning: left-hand operand of comma expression has no effect [-Wunused-value]
for (; o > 0, n; c--, n /= 10) {
^


Here's what the supplied main outputs on my machine. The first part is a demonstration of what happens when maxlen (which doesn't count the '') is too short (no buffer overflows! yay!):



"3"
"23"
"234"
",234"
"1,234"

"123"
"12,345"
"123,456"
"1,234,567"
"12,345,678"
"123,456,789"
"1,234,567,890"
"12,345,678,901"
"123,456,789,012"
"1,234,567,890,123"
"12,345,678,901,234"
"123,456,789,012,345"
"1,234,567,890,123,456"
"12,345,678,901,234,567"
"123,456,789,012,345,678"
"1,234,567,890,123,456,789"
"12,345,678,901,234,567,890"


The part I'm mostly... fascinated? with is the magic 3.1 near the top which is calculating what the length of the output string with commas added will be (the code of course works backwards). 3.1 Seems To Work™ for the values I'm supplying, but I'm 100.0% sure it's probably secretly horribly broken on some large inputs (which I've not figured out how to test yet).



There are also probably a myriad of other issues I've not thought of in terms of coding semantics and style.










share|improve this question









New contributor




i336_ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$




Beginner/occasional C tinkerer here. (Reasonably comfortable with scripting languages, tend to avoid C due to its terseness, but trying to work past that.)



I happened to want to insert commas into some integers. I got curious about various interesting (non-string-based) ways one might achieve that, found how to arithmetically reverse a string, and ended up writing this code.



#include <stdio.h>

int fmtn(unsigned long long n, char *buf, int maxlen)

int l = 0;
for (unsigned long long x = n; x; x /= 10, l++);

int l2 = l + (int)((l / 3.1));
if (l2 > maxlen) l2 = maxlen;
if (l > maxlen) l = maxlen;

int o = l2 - 1;
int c = l - 1;

for (; o > 0, n; c--, n /= 10)
if (o >= 0) buf[o--] = (n % 10) + 48;
if (o >= 0 && (l - c) % 3 == 0) buf[o--] = ',';


buf[l2] = '';

return l2;



#define test(x) do fmtn(x, b, 30); printf(""%s"n", b); while(0);
#define testn(x, n) do fmtn(x, b, n); printf(""%s"n", b); while(0);

int main(int argc, char **argv)

char b[30];

testn(123, 1);
testn(123, 2);
testn(1234, 3);
testn(1234, 4);
testn(1234, 5);

puts("");

test(123);
test(12345);
test(123456);
test(1234567);
test(12345678);
test(123456789);
test(1234567890);
test(12345678901);
test(123456789012);
test(1234567890123);
test(12345678901234);
test(123456789012345);
test(1234567890123456);
test(12345678901234567);
test(123456789012345678);
test(1234567890123456789);
test(12345678901234567890ull);





Compiling with -Wall produces this interesting message - I'm pretty sure that o will routinely <= 0:



In function 'fmtn':
15:14: warning: left-hand operand of comma expression has no effect [-Wunused-value]
for (; o > 0, n; c--, n /= 10) {
^


Here's what the supplied main outputs on my machine. The first part is a demonstration of what happens when maxlen (which doesn't count the '') is too short (no buffer overflows! yay!):



"3"
"23"
"234"
",234"
"1,234"

"123"
"12,345"
"123,456"
"1,234,567"
"12,345,678"
"123,456,789"
"1,234,567,890"
"12,345,678,901"
"123,456,789,012"
"1,234,567,890,123"
"12,345,678,901,234"
"123,456,789,012,345"
"1,234,567,890,123,456"
"12,345,678,901,234,567"
"123,456,789,012,345,678"
"1,234,567,890,123,456,789"
"12,345,678,901,234,567,890"


The part I'm mostly... fascinated? with is the magic 3.1 near the top which is calculating what the length of the output string with commas added will be (the code of course works backwards). 3.1 Seems To Work™ for the values I'm supplying, but I'm 100.0% sure it's probably secretly horribly broken on some large inputs (which I've not figured out how to test yet).



There are also probably a myriad of other issues I've not thought of in terms of coding semantics and style.







beginner c formatting integer






share|improve this question









New contributor




i336_ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




i336_ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 6 mins ago









200_success

131k17157422




131k17157422






New contributor




i336_ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 6 hours ago









i336_i336_

97




97




New contributor




i336_ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





i336_ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






i336_ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • $begingroup$
    The warning tells you, that your if condition does not work as you would expect. Try something like for (; o > 0 && n > 0; c--, n /= 10), then the warning will go away (see also this).
    $endgroup$
    – Alex
    6 hours ago











  • $begingroup$
    @Alex: OH. It's telling me , != &&, which I do indeed want here. (Compilers are cool; glad I didn't succumb to instinct and presume.) Thanks! Fixed. [Edit: Fixed in my version, that is; should I fix the version here too?]
    $endgroup$
    – i336_
    6 hours ago







  • 2




    $begingroup$
    If you're looking at the guidelines here, you will see that your question is off-topic until its properly working. So yes, fix your question, remove the paragraph about the warning and then your code is ready to be reviewed.
    $endgroup$
    – Alex
    5 hours ago










  • $begingroup$
    It would also be nice to explain a bit about how the code does what it does.
    $endgroup$
    – user673679
    3 hours ago
















  • $begingroup$
    The warning tells you, that your if condition does not work as you would expect. Try something like for (; o > 0 && n > 0; c--, n /= 10), then the warning will go away (see also this).
    $endgroup$
    – Alex
    6 hours ago











  • $begingroup$
    @Alex: OH. It's telling me , != &&, which I do indeed want here. (Compilers are cool; glad I didn't succumb to instinct and presume.) Thanks! Fixed. [Edit: Fixed in my version, that is; should I fix the version here too?]
    $endgroup$
    – i336_
    6 hours ago







  • 2




    $begingroup$
    If you're looking at the guidelines here, you will see that your question is off-topic until its properly working. So yes, fix your question, remove the paragraph about the warning and then your code is ready to be reviewed.
    $endgroup$
    – Alex
    5 hours ago










  • $begingroup$
    It would also be nice to explain a bit about how the code does what it does.
    $endgroup$
    – user673679
    3 hours ago















$begingroup$
The warning tells you, that your if condition does not work as you would expect. Try something like for (; o > 0 && n > 0; c--, n /= 10), then the warning will go away (see also this).
$endgroup$
– Alex
6 hours ago





$begingroup$
The warning tells you, that your if condition does not work as you would expect. Try something like for (; o > 0 && n > 0; c--, n /= 10), then the warning will go away (see also this).
$endgroup$
– Alex
6 hours ago













$begingroup$
@Alex: OH. It's telling me , != &&, which I do indeed want here. (Compilers are cool; glad I didn't succumb to instinct and presume.) Thanks! Fixed. [Edit: Fixed in my version, that is; should I fix the version here too?]
$endgroup$
– i336_
6 hours ago





$begingroup$
@Alex: OH. It's telling me , != &&, which I do indeed want here. (Compilers are cool; glad I didn't succumb to instinct and presume.) Thanks! Fixed. [Edit: Fixed in my version, that is; should I fix the version here too?]
$endgroup$
– i336_
6 hours ago





2




2




$begingroup$
If you're looking at the guidelines here, you will see that your question is off-topic until its properly working. So yes, fix your question, remove the paragraph about the warning and then your code is ready to be reviewed.
$endgroup$
– Alex
5 hours ago




$begingroup$
If you're looking at the guidelines here, you will see that your question is off-topic until its properly working. So yes, fix your question, remove the paragraph about the warning and then your code is ready to be reviewed.
$endgroup$
– Alex
5 hours ago












$begingroup$
It would also be nice to explain a bit about how the code does what it does.
$endgroup$
– user673679
3 hours ago




$begingroup$
It would also be nice to explain a bit about how the code does what it does.
$endgroup$
– user673679
3 hours ago










0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






i336_ is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217623%2fformatting-integers-with-commas-12345-%25e2%2586%2592-12-345%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes








i336_ is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















i336_ is a new contributor. Be nice, and check out our Code of Conduct.












i336_ is a new contributor. Be nice, and check out our Code of Conduct.











i336_ is a new contributor. Be nice, and check out our Code of Conduct.














Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217623%2fformatting-integers-with-commas-12345-%25e2%2586%2592-12-345%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

格濟夫卡 參考資料 导航菜单51°3′40″N 34°2′21″E / 51.06111°N 34.03917°E / 51.06111; 34.03917ГезівкаПогода в селі 编辑或修订