e3 Real Estate Website Templates

Welcome Guest (Register -  Login)  Active Topics Active Topics Display List of Forum Members Memberlist Search The Forum Search Help Help
 Navigation
 Skin Chooser

Choose Skin:


There are 2 skins total.
The newest skin is Classis Default

 Latest Forum Posts

Strings, Text, and Dates
 C++ GamesStrings, Text, and Dates
Subject Topic: how to truncate? Post ReplyPost New Topic
 how to truncate?
<< Prev Topic | Next Topic >>
fuzz
Posted: 10-January-2007 at 7:01pm | IP Logged Quote fuzz
Avatar
Newbie
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
View fuzz's Profile Search for other posts by fuzz Back to Top
 
neWWe}{
Posted: 11-January-2007 at 5:55am | IP Logged Quote neWWe}{
Avatar
Dedicated Groupie
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
View neWWe}{'s Profile Search for other posts by neWWe}{ Back to Top
 
fuzz
Posted: 11-January-2007 at 8:11pm | IP Logged Quote fuzz
Avatar
Newbie
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
View fuzz's Profile Search for other posts by fuzz Back to Top
 
neWWe}{
Posted: 12-January-2007 at 1:09pm | IP Logged Quote neWWe}{
Avatar
Dedicated Groupie
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
View neWWe}{'s Profile Search for other posts by neWWe}{ Back to Top
 
DutchDude
Posted: 19-June-2007 at 11:49am | IP Logged Quote DutchDude
Avatar
Super Dedicated Groupie
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
View DutchDude's Profile Search for other posts by DutchDude Visit DutchDude's CppGames Back to Top
 
em1189
Posted: 13-September-2007 at 3:41pm | IP Logged Quote em1189

Newbie
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
View em1189's Profile Search for other posts by em1189 Back to Top
 
m4ster_r0shi
Posted: 27-March-2010 at 7:30am | IP Logged Quote m4ster_r0shi

Newbie
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
View m4ster_r0shi's Profile Search for other posts by m4ster_r0shi Back to Top
 
1 User(s) are browsing this topic, 1 Guest(s) and 0 Member(s)
0 Members:
<< Prev Topic Strings, Text, and Dates Next Topic >>

If you wish to post a reply to this topic you must first login
If you are not already registered you must first register

  Post ReplyPost New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum



This page was generated in 0.2031 seconds.

[ Users browsing page: none ]



Terms and Conditions | Privacy | Contact Us

Copyright © 2010 Cpp Games. All rights reserved.

This site is best viewed at 1024x768 screen resolution.
Back to Top

Designed by Micronet Technologies