Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8538645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:10:06+00:00 2026-06-11T11:10:06+00:00

I use the MPFR library to do calculations on big numbers, but also return

  • 0

I use the MPFR library to do calculations on big numbers, but also return a double with 8 digits after the decimal point.

I mpfr_sprintf the number to a char array so precision or anything isn’t lost.
Everything is fine except that I didn’t find any thousand separator option in the documentation(or I missed it).

Given a number such as 20043.95381376 I would like to represent it like 20,043.95381376 for better readability.

Or the number 164992818.48075795 as 164,992,818.48075795

I read about an apostrophe that should be added to printf/sprintf, but that seems to be a UNIX/POSIX thing and I am a Windows user.

Since internally I print the number as a string, I thought what I could do is write a custom implementation that would automatically add the comma depending on the number(>1000>10000>100000 and so forth) but then I realized that functions like strncpy or strcpy will essentially replace, not add the comma to the desired position. And here is how I am back to square one on how to do it.

How can I do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:10:08+00:00Added an answer on June 11, 2026 at 11:10 am

    You need your implementation to convert the double value to string and examine each character of that string, then copy it to an output string along with the separators.

    Something like this:

    #include <stdio.h>
    #include <string.h>
    
    int thousandsep(double in, char* out_str, size_t out_len, unsigned int precision) {
        char in_str[128], int_str[128], format[32];
        size_t dlen, mod, i, j;
        int c;
    
        snprintf(format, sizeof format, "%%.%df", precision);
        snprintf(in_str, sizeof in_str, format, in);
        snprintf(int_str, sizeof int_str, "%d", (int)in);
    
        dlen = strlen(in_str);
        mod = strlen(int_str) % 3;
        c = (mod == 0) ? 3 : mod;
    
        for (i=0, j=0; i<dlen; i++, j++, c--) {
            if (j >= out_len - 1) {
                /* out_str is too small */
                return -1;
            }
    
            if (in_str[i] == '.') {
                c = -1;
            } else if (c == 0) {
                out_str[j++] = ',';
                c = 3;
            }
    
            out_str[j] = in_str[i];
        }
        out_str[j] = '\0';
    
        return 0;
    }
    

    Then use it like so:

    char out_str[64];
    
    if (thousandsep(20043.95381376, out_str, sizeof out_str, 8) == 0)
        printf("%s\n", out_str);       /* 20,043.95381376 */
    
    if (thousandsep(164992818.48075795, out_str, sizeof out_str, 8) == 0)
        printf("%s\n", out_str);       /* 164,992,818.48075795 */
    
    if (thousandsep(1234567.0, out_str, sizeof out_str, 0) == 0)
        printf("%s\n", out_str);       /* 1,234,567 */
    

    Note: I assumed that if you’re on Windows, you may be using MSVC so this solution should be working on C89 compilers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

use a reapeter to display data, but sometimes the data is to big to
Use to work with a MySQL + + (library for C + +) The
Use case: we have some project meta-data files which we want tracked, but are
The program saves a settings file as text which contains floating point numbers of
Use case: User clicks a link, a modal window is displayed containing one big
Use new MediaPlayer by Yahoo. Content parse automatic - greate, but I use ajax
Use of rails 3 recaptcha. In view _form.html.erb insert <%= recaptcha_tags %> But when
use messagepack on android, the can serialize/deserialize a class, but not absolutely right .
Use Case Show a photo uploaded by the user in a square box with
use C#,want to upload excel file on google doc. bellow syntax use to upload

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.