Solved

output string formatting

Posted on 1998-09-09
7
277 Views
Last Modified: 2010-04-10
Hi, in C one uses " %4.3lf " to string format a long double floating point value. And " %4.3f " for ordinary floating point values.

What is its  equivalent in C++ ? (i.e. for long double string formating)

I tried using setprecision(4), but this messes up the output which returns with a string of # signs and some alphanumeric characters.

setprecision() is meant for normal/ordinary floating point values, but I don't know which is the equivalent parameter for long double floating point values .

Thanks

Paul
0
Comment
Question by:pdorai
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 6

Accepted Solution

by:
WxW earned 30 total points
ID: 1172316
The same . Functions included in stdio.h as printf are available to C++ . Furthermore , if your app is a windows app , use sprintf to create a buffer to format the text
0
 
LVL 22

Expert Comment

by:nietod
ID: 1172317
WxW, I think pdorai is trying to use streams.  Thus, the converting back to the C way is not a good idea.

setprecision(4) is not 100% equiviliant to C's %4.3f.  the precision is used in two ways.  If the stream is in automatic format or scientific format, then it will display 4 significant digits.  If the stream is in fixed format it will display 4 decimal digits.  You may need to set the width prior to ouputing the number to get the desired result.  
0
 
LVL 6

Expert Comment

by:snoegler
ID: 1172318
In my opinion is every C function also a C++ function - but i think that's bad karma, right?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 22

Expert Comment

by:nietod
ID: 1172319
Yes, but in many cases C++ provides improved ways to perform tasks that are not available in C.  C++'s I/O streams are an example.  They are more powerful and are much safer to use.  
0
 
LVL 6

Expert Comment

by:WxW
ID: 1172320
C++ streams are more powerful than printf and sprintf ?
For creating simple buffers ... I think that they are better & easier
0
 
LVL 2

Expert Comment

by:Paullkha
ID: 1172321
Here's a couple of ways!

CString text; //string???
float value = 1.30;
text.Format("$%7.2f", value);
(I like the above because you can easily see the formatting AND you are not changing the setup for cout)
or

cout.precision(4);
cout << 1.3456;

or

ios_base::fixed

etc...


0
 
LVL 2

Expert Comment

by:Paullkha
ID: 1172322
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
      long double x;
      x = 12.3456;
      cout << setprecision(2) << fixed << setw(5) << x <<endl;
      return (0);
}
Is this what you want?
fixed is fixed decimal point
setprecision, as mentioned above, sets number of digits AFTER decimal point
setw sets the width to 5, this width INCLUDES the decimal point
Good luck
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now