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 8920529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:17:40+00:00 2026-06-15T06:17:40+00:00

Having char double[] = 1.2345678; , I have to trim all digits in 4nd

  • 0

Having char double[] = "1.2345678"; , I have to trim all digits in 4nd place and above after the . in this char * i.e make it to "1.234" .

  • 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-15T06:17:42+00:00Added an answer on June 15, 2026 at 6:17 am

    One obvious solution is to replace a character by NUL, like so:

    char *foo = strdup("1.2345678");   // FIXME: check return value
    foo[5] = '\0';
    

    Note that the exact position might differ, depening on how many digits appear before the ‘.’ character.

    Iterate over the string foo, change the state in passed_dot if you come across ‘.’, and insert a NUL after 4 more characters:

    char *p = foo;
    int i = 0;
    int passed_dot = 0;
    while (p && *p) {
            if (*p == '.') passed_dot = 1;
            if (passed_dot) i++;
            if (i == 4) {
                    *p = '\0';
                    break;
            }
    
            p++;
    }
    

    If you can’t afford to buy more RAM, you may strdup() the resulting string and free() the old one in order to save memory:

    new_str = strdup(foo);   // and don't forget to check for NULL
    free(foo);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having list of char array by that I want to store all
I am having a problem with precision of a double after performing some operations
This is really a double question, my two end goals having answers to: What
So I'm having a problem where I have an array of char*s (declared as)
I am having difficulty in understanding IPC in multiprocess system. I have this system
I have the following struct: typedef struct{ int vin; char* make; char* model; int
I have this method (Matrix::WriteToArray(double &CopyOfArray)) that I want to write a copy of
I'm having a char(4) FK reltionship, and it gives me lots of errors when
Having code: Date::Date(const char* day, const char* month, const char* year):is_leap__(false) { my_day_ =
I'm having a problem and can't seem to find the solution.. int linearSearch(nodeptr list,char

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.