943,748 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4474
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 3rd, 2009
0

how to round off using float

Expand Post »
i don't know how to make 1.6666667 to 1.6 only.. .

please someone help me. .

please! please! please!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
taggz19 is offline Offline
2 posts
since Jul 2009
Jul 3rd, 2009
0

Re: how to round off using float

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.
Reputation Points: 485
Solved Threads: 88
Posting Pro
csurfer is offline Offline
564 posts
since Jan 2009
Jul 3rd, 2009
0

Re: how to round off using float

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. . .
Reputation Points: 10
Solved Threads: 0
Newbie Poster
taggz19 is offline Offline
2 posts
since Jul 2009
Jul 4th, 2009
0

Re: how to round off using float

You could do this:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 22
Solved Threads: 0
Newbie Poster
prime1999 is offline Offline
18 posts
since Jul 2009
Jul 4th, 2009
2

Re: how to round off using float

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)
cpp Syntax (Toggle Plain Text)
  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:
cpp Syntax (Toggle Plain Text)
  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.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Jul 4th, 2009
0

Re: how to round off using float

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;
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jul 4th, 2009
1

Re: how to round off using float

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:
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 22
Solved Threads: 0
Newbie Poster
prime1999 is offline Offline
18 posts
since Jul 2009
Jul 4th, 2009
-1

Re: how to round off using float

use the iomanip header

c++ Syntax (Toggle Plain Text)
  1. #include <iomanip.h>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. cout << "enter two numbers that you want to divide >> ";
  9. double a;
  10. double b;
  11. cin >> a;
  12. cin >> b;
  13. cout.precision(2); // only display 2 numbers
  14. cout << a << " / " << b << "= " << a/b;
  15. cin.get();
  16. cin.get();
  17. return 0;
  18. }

this will give u two number in precision.
Last edited by Nick Evan; Jan 25th, 2010 at 11:58 am.
Reputation Points: 6
Solved Threads: 2
Light Poster
kangarooblood is offline Offline
42 posts
since Jun 2009
Jul 6th, 2009
0

Re: how to round off using float

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.
Reputation Points: 22
Solved Threads: 0
Newbie Poster
prime1999 is offline Offline
18 posts
since Jul 2009
Jan 25th, 2010
-4
Re: how to round off using float
I think it to not possible to round off 1.666666 to 1.6
the only thing u can do is u can restrict the number of digits u display after floating by
i=1.66666
printf("%0.1f",i);

output
1.6
Reputation Points: 7
Solved Threads: 0
Newbie Poster
kdeepak is offline Offline
3 posts
since Jan 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C++ Forum Timeline: c++ os?
Next Thread in C++ Forum Timeline: isdigit() or isalpha() help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC