Solved

output string formatting

Posted on 1998-09-09
7
281 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
Independent Software Vendors: 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

756 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