how to round off using float

Reply

Join Date: Jul 2009
Posts: 2
Reputation: taggz19 is an unknown quantity at this point 
Solved Threads: 0
taggz19 taggz19 is offline Offline
Newbie Poster

how to round off using float

 
0
  #1
Jul 3rd, 2009
i don't know how to make 1.6666667 to 1.6 only.. .

please someone help me. .

please! please! please!
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: how to round off using float

 
0
  #2
Jul 3rd, 2009
You can do these :

1>Well if its just a matter of printing then "%.nf" where n is the number of digits after the "." you want to print in a floating point number.

2>If its really about conversion then you can know the IEEE floating point format for your machine and compiler architecture and then mask off the excessive bits by "logical anding" which ofcourse gets you into some troubles as you don't exactly know the number stored there and its stored in
<1bit-signbit><8bit-signed exponent><23bit-mantissa> for single precision 32 bit float format.

3>So may be you can do this :

* char num[10];
* sprintf(num,"%1f",<your actual float variable>);
* Now traverse through the array num[] and get the digits before "." the "." and digits after "." and then convert it into a number.

If there are any other ways even I would like to know.
Last edited by csurfer; Jul 3rd, 2009 at 11:37 pm.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 2
Reputation: taggz19 is an unknown quantity at this point 
Solved Threads: 0
taggz19 taggz19 is offline Offline
Newbie Poster

Re: how to round off using float

 
0
  #3
Jul 3rd, 2009
thanks for the info, but when i enter "%.1f" it results into 1.7 and not 1.6. .

what should i do??

please help me. . .
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 9
Reputation: prime1999 is an unknown quantity at this point 
Solved Threads: 0
prime1999 prime1999 is offline Offline
Newbie Poster

Re: how to round off using float

 
0
  #4
Jul 4th, 2009
You could do this:
  1. // #include<cstring>
  2. float t=31.4592;
  3. char num[100];
  4. sprintf(num,"%f",t);
  5. p=strchr(num,'.');
  6. if( p!=NULL ) num[p-num+2]=0;
  7. printf("%s\n",num);
or this:
  1. // #include<cmath>
  2. float t=31.4592;
  3. printf("%.1f\n",floor(t*10)/10);
Last edited by prime1999; Jul 4th, 2009 at 1:19 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,592
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: how to round off using float

 
1
  #5
Jul 4th, 2009
if you want to round the number, then you use "setprecision(<n>)" or the format specifier "%.<n>f" for "printf()" where <n> is the number of digits you want to round to. E.g., the value 1.66667 rounded to <n>=1 decimal place will be 1.7

but what you really want to do here is truncate. this means just "chopping off" (so to speak) all digits past the desired place will just be thrown out, and the least significant remaining digit will not be rounded.

to truncate a value, then, you need to use the "floor" function (for positive values, see caveat* below). but since this returns only a whole number, you will need to first "shift" the value to be truncated a number of decimal places that you want to truncate, floor the value, then shift them back.

(note my use of the word "shift" here is not a bitwise shift, because we're talking about decimal places, not binary places -- you will have to multiply by powers of 10)
  1. value = 1.66667;
  2. truncValue = value * pow(10,1); // shifts dec. pt. 1 place right
  3. truncValue = floor(truncValue);
  4. truncValue /= pow(10,1); // shifts dec. pt. 1 place left
you can replace the '1' with a variable to allow variable truncation.

*caveat: this will not work for negative numbers. example value = -1.666667 rounded to 1 decimal point would be -1.7, and truncated to 1 decimal point should be -1.6.

However, if you used "floor" on this negative value, the result would be -1.7. Therefore, negative values need to use the converse function "ceil()" which would give the expected value -1.6.

now you can make a function like so:
  1. double truncate (double value, int places)
  2. {
  3. if (value>0)
  4. {
  5. // do the routine with "floor"
  6. }
  7. else
  8. {
  9. // do the routine with "ceil"
  10. }
  11.  
  12. return truncValue;
  13. }
Last edited by jephthah; Jul 4th, 2009 at 1:57 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: how to round off using float

 
0
  #6
Jul 4th, 2009
The float to ASCII, clip, then ASCII to float is one method.
You're only truncating by one decimal place! Not I said clip not round.

So one method is
  1. f = floor(N * 10.0f) * 0.1f;
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 9
Reputation: prime1999 is an unknown quantity at this point 
Solved Threads: 0
prime1999 prime1999 is offline Offline
Newbie Poster

Re: how to round off using float

 
1
  #7
Jul 4th, 2009
Yeah I didn't realise your "caveat" that negative values would be truncated wrongly... but if you want an easier way other than if loops:
  1. // #include<cmath>
  2. float t=31.4592,x;
  3. modf(t*10,&x);
  4. printf("%.1f\n",x/10);
Last edited by prime1999; Jul 4th, 2009 at 9:39 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 38
Reputation: kangarooblood is an unknown quantity at this point 
Solved Threads: 2
kangarooblood kangarooblood is offline Offline
Light Poster

Re: how to round off using float

 
-1
  #8
Jul 4th, 2009
use the iomanip header

[code=c++]
#include <iomanip.h>
#include <iostream>

using namespace std;

int main()
{
cout << "enter two numbers that you want to divide >> ";
double a;
double b;
cin >> a;
cin >> b;
cout.precision(2); // only display 2 numbers
cout << a << " / " << b << "= " << a/b;
cin.get();
cin.get();
return 0;
}
[code]

this will give u two number in precision.
// Maybe you like C++ put I prefer A++...
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 9
Reputation: prime1999 is an unknown quantity at this point 
Solved Threads: 0
prime1999 prime1999 is offline Offline
Newbie Poster

Re: how to round off using float

 
0
  #9
Jul 6th, 2009
Look at post #3... the original poster seems to want to truncate the float to 1 dp.

And please close your code tag properly!
Last edited by prime1999; Jul 6th, 2009 at 2:08 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC