C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 24th, 2003, 07:33 AM
AGibel's Avatar
AGibel AGibel is offline
Bad Andy
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: OH
Posts: 275 AGibel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
sig figs BCB

I know in console C++ iomanip and setprecision could be used to set the number of significant figures to be displayed. However, this was for use with the cout << operator. How would I make say, a double be rounded to 2 sig figs after the decimal? Is it possible to set the value inside the variable?

SomeFunction(double) = double

Or is there another way of formatting numbers? As always, any help is appreciated. Thanks.

Reply With Quote
  #2  
Old June 24th, 2003, 12:22 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is online now
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,589 Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 58 m 25 sec
Reputation Power: 1001
Something like this maybe:
Code:
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;

double roundit(double val, int places) {
  return (floor(val * pow(10, places) + 0.5) / pow(10, places));
}

int main(void) {
  double foo = 123.456789;
  double bar = roundit(foo, 2);
  cout << setprecision(10) << foo << " " << bar << endl;

  return 0;
}

Reply With Quote
  #3  
Old June 24th, 2003, 12:35 PM
dcaillouet's Avatar
dcaillouet dcaillouet is offline
Big Endian
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: May 2001
Location: Fly-over country
Posts: 1,173 dcaillouet User rank is Sergeant (500 - 2000 Reputation Level)dcaillouet User rank is Sergeant (500 - 2000 Reputation Level)dcaillouet User rank is Sergeant (500 - 2000 Reputation Level)dcaillouet User rank is Sergeant (500 - 2000 Reputation Level)dcaillouet User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 16 h 29 m 5 sec
Reputation Power: 24
You could still use streams (and iomanip) even if you're not doing a console application:
Code:
#include <sstream>
#include <string>
using namespace std;
.
.
.
__fastcall TForm1::TForm1(TComponent* Owner)    : TForm(Owner)
{
   ostringstream oss;
   string sTest;

   oss << "AGibel" << " Testing " << 123;
   sTest = oss.str();
   Edit1->Text = sTest.c_str();
}
Another method would be to use the CurrToStrF function for formatting currency:

CurrencyString = ""; // instead of "$"
CurrencyFormat = 3;
NegCurrFormat = 15;
ThousandSeparator = ''; // instead of ','
DecimalSeparator = '.';

double dblValue = 1234.5678;
Amount->Text = CurrToStrF(dblValue, ffNumber, 2);

You also might want to look at the sprintf() method of an AnsiString. See page 3 and 4 of the attached document.
Attached Files
File Type: zip vclmathfunctions.zip (289.7 KB, 216 views)

Reply With Quote
  #4  
Old June 25th, 2003, 07:32 AM
AGibel's Avatar
AGibel AGibel is offline
Bad Andy
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: OH
Posts: 275 AGibel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
OK, thanks. I'll try some of those out.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > sig figs BCB


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT