Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

output string formatting

Posted on 1998-09-09
7
Medium Priority
?
290 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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…
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.

715 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