Displaying Long Double

Posted on 1997-05-01
Last Modified: 2008-03-10
I've declared a long double variable and tried to display it but could only display in exponential format.

The problem is, C allows me to declare a long double but printf and sprintf only handles up to the size of double (16 digits).

I need to at display the full value on the screen as well as converting it to a string using sprintf. To illustrate:

/* Dummy codes */
long double buf1, buf2, result;
char ans[20];

buf1 = 12345678901234567.00;
buf2 = 12345678901234567.00;
result = buf1+buf2;
printf("result: %17.0f", result);
sprintf(ans, "%17.0f", result);

If printf and sprintf can't work please suggest other alternatives.

Note: The maximum length of the variable "result" can go up to 18 digits.

Please help, this is urgent. Thank you.
Question by:samtan

Expert Comment

ID: 1250218
(1) Long double is not ANSI C. It is an extension.
(2) On my machine I can only get 17 significant figues for long double. You may be asking for more than is available.
(3) Are you only using whole numbers? If so:
(3.1) consider looking at one of the multiple-precision-integer packages that are around.
(3.2) consider using "long long integer" which gcc (at least) supports.
Both of these enable you to use 64 bit integers, which give up to 20 decimal digits. (If you are on a Digital Alpha they are native).

Accepted Solution

AVaulin earned 100 total points
ID: 1250219
Try to use "fcvt" function. Prototype is in "stdlib.h".

Expert Comment

ID: 1250220
No, fcvt takes a double argument.

You can use (s)printf in some systems.  It's not part of the ANSI spec for (s)printf though.  In GNU library systems (including Linux), you can use the 'L' prefix (yes, upper case).  In BSD, you can use 'q'.  Actually, you can use 'q' in both, so that's the more portable (but not Portable) way of doing it.

The follow code works fine on Linux:

#include <stdio.h>

int main()
  long double a,b,c;
  printf("result: %17.0qf\n", c);  /* Or Lf */
  return 0;

If this is any good to you, reject the above and I'll enter an empty answer for you to accept.



Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question