ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
straightforward question..
just wondering if their is a standard c++ header that allows you to return a double with 2 decimal places.
Thanks
edit: without using printf()
its a c++ project so im using cout. i wouldnt want to use cout for the entire program then printf for that one line, it shouldnt be necessary.
is there a link to this round header file? either way, it would probably act like ceil or floor and wouldnt do what i am looking for here, i would think.
i just picked the name round because i couldnt think of anything at the moment.. and this project is my first c++ program, and i wouldnt be releasing it at all, so.
You can use setprecision to set the number of significant digits displayed for a floating point number. That includes the number of digits in front of the decimal point as well, though. For example:
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float f = 100.123456;
cout << setprecision(4) << f << endl;
}
Output:
100.1
I'm looking more to see if there is a way to set the precision similar to the C way, which I have usually reverted to. You could always use sprintf to put the result in a C-style string, then use that C-Style string in a C++ string object, or cout or whatever.
For instance:
Code:
char buffer[256];
float f = 100.1234567;
std::string s;
sprintf(buffer, "%.2f", f); // results in 100.12
s = buffer;
Ok, I figured out a way you can do it using only C++ streams.
Code:
float f = 100.123456789;
cout << setprecision(2) << fixed << f << endl;
Output:
100.12
The good thing about this method is that if you have, say 1.2. It'll output 1.20. It doesn't appear that your round method takes that into account, but maybe you don't have any numbers with fewer than 2 decimal places so it doesn't matter.
perfect deli!
Thanks alot, thats just what i need! too bad i already wrote mine, and, as you mentioned, no it doesnt take care of the XXX.X (1 decimal place) scenario, but again as you said, i havent seen a number like that in the program so far.. but ill probably go with the method you showed as its most likely faster and works better!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.