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

  • SEARCH
  • Home
  • 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 7847265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:50:31+00:00 2026-06-02T17:50:31+00:00

I have one specific problem while converting double values to string using sprintf() in

  • 0

I have one specific problem while converting double values to string using sprintf() in UNIX.

For example I have two values:

double a = 0.009984354523452;
double b = 0.01;

While converting, I am using:

sprintf(somestringvar, "Double value : %.15f \n", a);
sprintf(diffstringvar, "Double value : %.15f \n", b);

to convert to a string.

My problem is for the ‘a’, the value is printing properly but for the value of ‘b’, 0’s are appended at the tail end. Please provide me any common way to represent ‘a’ and ‘b’ as exact values.

  • 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-02T17:50:33+00:00Added an answer on June 2, 2026 at 5:50 pm

    You get zeros when printing b because you told printf to print 15 decimal places (%.15f), and it is obediently doing so. To get something “exact”, you might have better luck with simple %g.


    [Update based on recent comments]

    Things really depend on what you mean by “exact”. If by exact, you mean “what the user originally entered”, then a double might not be the best choice for you. Perhaps storing the value as a string and then converting it to a double when you need to use it in a computation might work. Once a text-based decimal number is converted to a double, it generally is no longer exactly the same as the text. While printf() or ostream will dutifully print exactly what the double holds (and sometimes it gets lucky and what it prints looks the same as the original value), the problem is that the double itself no longer holds the actual original value. And in fact it never did.

    Alternatively, you might favor a precise numeric implementation rather than something inherently approximate like doubles. For example, MySQL has NUMERIC and DECIMAL types that support perfect precision at the cost of forcing you to specify the number of integer and decimal places ahead of time. This is called arbitrary precision arithmetic and there are libraries for doing this in C/C++.

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

Sidebar

Related Questions

Given that two branches have diverged and a specific commit from one branch (and
I have two development machines. One machine is setup to use a specific Mercurial
I have one table TABLE_EXAM . i need to update the value of specific
I have seen a few posts regarding this issue but not one specific to
I have some sort of a jquery chat at one specific PHP page. Of
I have more than one Event Listener that calls a specific function, and I
I have a data frame where one particular column has a set of specific
I have multiple images inside one form. Depending on the image clicked a specific
With cvs one can export the files that have a specific tag and get
I'm using urllib2 to interact with a webserver. For the specific problem I need

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.