2 Replies - 749 Views - Last Post: 19 October 2009 - 12:46 AM Rate Topic: -----

#1 ethereal1m  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 214
  • Joined: 30-June 09

Display a string that is converted from long double in decimal form

Post icon  Posted 18 October 2009 - 10:29 PM

Greetings coders,
My code converts a long double number into a string. When it display the string, it shows in scientific format. How can I make the function shows the string in decimal format? Can I use setiosflag for the decimal formating? If I can use it, how?

#include <iostream>
#include <sstream> 
#include <iomanip>
using namespace std;

string convertToString(long double MyValue)
{
	ostringstream OutStream;
	// try to use setiosflags but it doesn't work 
	cout<<setiosflags(ios::fixed)<<setiosflags(ios::showpoint)
		<<setprecision(2);
	OutStream << MyValue;
   	return (OutStream.str());
}

void main() {
	long double input=456743332;
	string output;
	output=convertToString(input);
	cout<<"output is "<<output<<endl;
}



much appreciated,
ethereal

Is This A Good Question/Topic? 0
  • +

Replies To: Display a string that is converted from long double in decimal form

#2 Guest_c.user*


Reputation:

Re: Display a string that is converted from long double in decimal form

Posted 18 October 2009 - 11:59 PM

 
#include <iostream>
#include <sstream>

using namespace std;

string Get(long double n);

int main(void)
{
	long double n;
	
	n = 456743332.123456;
	
	cout << Get(n) << endl;
	
	return 0;
}

string Get(long double n)
{
	ostringstream os;
		
	os.setf(ios::fixed);
	os.precision(2);
	os << n;
	return os.str();
}


Was This Post Helpful? 1

#3 ethereal1m  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 214
  • Joined: 30-June 09

Re: Display a string that is converted from long double in decimal form

Posted 19 October 2009 - 12:46 AM

hey, cool, thx this works....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1