LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-11-2005, 09:18 PM   #1
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
how to print 2 decimal places of a double?


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()

Last edited by nadroj; 07-11-2005 at 09:51 PM.
 
Old 07-11-2005, 09:50 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Original Poster
Rep: Reputation: 60
nevermind, i made my own
Code:
string round(string i)
{
       return i.substr(0, strcspn(i.c_str(),".") + 3);
}
 
Old 07-12-2005, 09:36 AM   #3
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Is there some reason you don't want to use a standard library call -- fprintf printf sprintf?

All this means is that two years from now nobody is going to know why your "round" is there.

You do know round is part of the standard library - you should pick another name for yours.
 
Old 07-12-2005, 10:15 AM   #4
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Original Poster
Rep: Reputation: 60
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.
 
Old 07-12-2005, 10:33 AM   #5
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
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;

Last edited by deiussum; 07-12-2005 at 10:34 AM.
 
Old 07-12-2005, 10:48 AM   #6
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
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.

Last edited by deiussum; 07-12-2005 at 10:53 AM.
 
Old 07-12-2005, 11:03 AM   #7
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Original Poster
Rep: Reputation: 60
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!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Decimal to binary maldini1010 Programming 6 02-01-2005 04:03 PM
limit result of float to two decimal places Strus Programming 3 12-17-2004 08:42 PM
Print one page get double tarheel92x Linux - Networking 0 02-11-2004 05:15 PM
perl print double in proper format rajatgarg Programming 1 01-30-2004 12:57 PM
How do I print double sided output rdaves@earthlink.net Linux - General 1 06-27-2001 02:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration