LinuxQuestions.org
Review your favorite Linux distribution.
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 11-01-2005, 07:01 AM   #1
TruongAn
Member
 
Registered: Dec 2004
Location: Vietnam (Việt Nam)
Distribution: Gentoo (desktop), Arch linux (laptop)
Posts: 728

Rep: Reputation: 33
How to format a long double number in C++ to display as many digits as possible.


Hi guys.
I am studying C++.
And I have wrote a program which calculate with long double number.
My goal is not to dis play the point in the number.
I mean: dislay
Code:
199999
not
Code:
2.00e+3
.
How can I do so with C++.
 
Old 11-01-2005, 10:30 AM   #2
naf
Member
 
Registered: Oct 2005
Location: Chicago, USA
Distribution: Slackware & Fedora
Posts: 66

Rep: Reputation: 15
Use a fixed point style and set the precision to 0:

Code:
#include <cstdio>
#include <iostream>

using namespace std;

int main( int argc, char **argv )
{
    double d = 1234.56789012345678901234567890;
    cout << "Double (raw) = " << d << endl;  // Defaults to 6 significant digits.
    cout.setf( ios_base::fixed, ios_base::floatfield );
    cout.precision( 0 );
    cout << "Double (adj) = " << d << endl;

    return 0;
}
Results:
Code:
Double (raw) = 1234.57
Double (adj) = 1235
 
Old 11-03-2005, 08:45 AM   #3
TruongAn
Member
 
Registered: Dec 2004
Location: Vietnam (Việt Nam)
Distribution: Gentoo (desktop), Arch linux (laptop)
Posts: 728

Original Poster
Rep: Reputation: 33
It work, thank you!
 
Old 11-08-2005, 11:19 AM   #4
TruongAn
Member
 
Registered: Dec 2004
Location: Vietnam (Việt Nam)
Distribution: Gentoo (desktop), Arch linux (laptop)
Posts: 728

Original Poster
Rep: Reputation: 33
Please explain the code for me, I am new to C++
 
Old 11-09-2005, 11:11 AM   #5
naf
Member
 
Registered: Oct 2005
Location: Chicago, USA
Distribution: Slackware & Fedora
Posts: 66

Rep: Reputation: 15
The code modifies the output stream directly for the subsequent write by changing the values of base_ios. If you want the change to remain persistent, you need to use manipulators. The concept is well documented in many books including Stroustrups. But for one good online resource look at:
http://www.cplusplus.com/ref/iostream/ios_base
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Configure: error: cannot compute sizeof (long double) in glibc pass 2 irfanali Linux From Scratch 29 02-03-2010 07:48 AM
double to long, messing with the value Ephracis Programming 10 06-08-2005 08:54 AM
Help! 2.4.29 + nptl = glibc error: cannot compute (long double), 77 Yerp Linux From Scratch 3 03-25-2005 07:28 PM
Fedora and the long double? robhd2 Programming 1 05-25-2004 01:18 PM
perl print double in proper format rajatgarg Programming 1 01-30-2004 01:57 PM

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

All times are GMT -5. The time now is 11:34 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration