Solved

output string formatting

Posted on 1998-09-09
7
276 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
Comment Utility
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
Comment Utility
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
Comment Utility
In my opinion is every C function also a C++ function - but i think that's bad karma, right?
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
#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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  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 …
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 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

10 Experts available now in Live!

Get 1:1 Help Now