| how to truncate? |
| << Prev Topic | Next Topic >> |
| fuzz |
| Posted: 10-January-2007 at 7:01pm | IP Logged
|
|
|
Newbie

Group: Newbie
Joined: 23-September-2006 Location: United States Posts: 14
|
My teachers said there was a code to truncate decimals, but didn't tell us how. By truncating decimals i mean like changing 5.25678 to 5.256. If anyone knows how please reply with the code. It doesn't matter the compiler. Thanks
|
Online Status: Offline
|
|
| |
| neWWe}{ |
| Posted: 11-January-2007 at 5:55am | IP Logged
|
|
|
Dedicated Groupie

Group: Dedicated Groupie
Joined: 07-January-2007 Location: France Posts: 44
|
if you mean truncate when using printf then you can use this printf("%.3f",f); //leaves only 3 decimals printf("%10.3f",f); //prints f on 10 positions & 3 decimals printf("%10f",f); //prints f on 10 positions
try to test to understand how it works
|
Online Status: Offline
|
|
| |
| fuzz |
| Posted: 11-January-2007 at 8:11pm | IP Logged
|
|
|
Newbie

Group: Newbie
Joined: 23-September-2006 Location: United States Posts: 14
|
what i need is a code that does not round but truncate its, ex. 5.376 truncated to the .01 would be 5.37, if it would be rounded it would display 5.38. What you game me rounds the number, but thanks for the help!
|
Online Status: Offline
|
|
| |
| neWWe}{ |
| Posted: 12-January-2007 at 1:09pm | IP Logged
|
|
|
Dedicated Groupie

Group: Dedicated Groupie
Joined: 07-January-2007 Location: France Posts: 44
|
then use this
void truncate(float *n, int d)
{
int t=1;
while(n) t*=10;
return ((float)( (int)(*n*t) ))/(float)t;
}
|
Online Status: Offline
|
|
| |
| DutchDude |
| Posted: 19-June-2007 at 11:49am | IP Logged
|
|
|
Super Dedicated Groupie

Group: Super Dedicated Groupie
Joined: 02-April-2007 Location: Netherlands Posts: 71
|
LOL, It's actually that simple Simplicity is my biggest friend, my only friend realy.
|
Online Status: Offline
|
|
| |
| em1189 |
| Posted: 13-September-2007 at 3:41pm | IP Logged
|
|
|
Newbie

Group: Newbie
Joined: 13-September-2007 Posts: 2
|
#include<iomanip>
cout << setpoint(n) << fixed
replace "n" with the number of decimal places you want, put this line in after your declaration statements and before your assignment statements....
edit: sorry just tested this and it does round the number
|
Online Status: Offline
|
|
| |
| m4ster_r0shi |
| Posted: 27-March-2010 at 7:30am | IP Logged
|
|
|
Newbie

Group: Newbie
Joined: 26-March-2010 Posts: 4
|
check this out:
----------------------------------------- -----------------------------------------
#include <iostream> #include <cmath> using namespace std;
double truncate(double number,int pos) { //this will hold the truncated value double truncated; // is our number positive or negative? int sign; sign=number>0?1:-1; //integer and decimal parts of our number unsigned int int_part; unsigned int dec_part; //work with absolute value number=fabs(number); //break down our number... int_part=number; int pot=pow(10.0,pos); //pot -> power of ten, LOL dec_part=(number-int_part)*pot; //...and build the truncated one truncated=int_part; truncated+=double(dec_part)/pot; //fix the sign truncated*=sign; return truncated; }
int main() { double d1=123.456; double d2=-456.321; int i; for (i=3; i>=0; i--) cout << "truncate(" << d1 << "," << i << ")=" << truncate(d1,i) << endl; for (i=3; i>=0; i--) cout << "truncate(" << d2 << "," << i << ")=" << truncate(d2,i) << endl; system("pause"); return 0; }
|
Online Status: Offline
|
|
| |
| 1 User(s) are browsing this topic, 1 Guest(s) and 0 Member(s) |
| 0 Members: |
|
|