Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

output string formatting

Posted on 1998-09-09
7
Medium Priority
?
294 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 60 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

783 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