| | |
how to round off using float
![]() |
You can do these :
1>Well if its just a matter of printing then
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
<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.
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"....
•
•
Join Date: Jul 2009
Posts: 9
Reputation:
Solved Threads: 0
You could do this:
or this:
C++ Syntax (Toggle Plain Text)
// #include<cstring> float t=31.4592; char num[100]; sprintf(num,"%f",t); p=strchr(num,'.'); if( p!=NULL ) num[p-num+2]=0; printf("%s\n",num);
C++ Syntax (Toggle Plain Text)
// #include<cmath> float t=31.4592; printf("%.1f\n",floor(t*10)/10);
Last edited by prime1999; Jul 4th, 2009 at 1:19 am.
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)
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:
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)
value = 1.66667; truncValue = value * pow(10,1); // shifts dec. pt. 1 place right truncValue = floor(truncValue); truncValue /= pow(10,1); // shifts dec. pt. 1 place left
*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)
double truncate (double value, int places) { if (value>0) { // do the routine with "floor" } else { // do the routine with "ceil" } return truncValue; }
Last edited by jephthah; Jul 4th, 2009 at 1:57 am.
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
You're only truncating by one decimal place! Not I said clip not round.
So one method is
C Syntax (Toggle Plain Text)
f = floor(N * 10.0f) * 0.1f;
•
•
Join Date: Jul 2009
Posts: 9
Reputation:
Solved Threads: 0
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)
// #include<cmath> float t=31.4592,x; modf(t*10,&x); printf("%.1f\n",x/10);
Last edited by prime1999; Jul 4th, 2009 at 9:39 am.
•
•
Join Date: Jun 2009
Posts: 38
Reputation:
Solved Threads: 2
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.
[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++...
![]() |
Similar Threads
- BGI graphics not supported in windows.. (C++)
- Help with Rounding 3 decimal places (C++)
- Drawing a Demi-Death Star: Questions About Bresenham's Line Algorithm in C++ (C++)
- ASAP - exe problem (C++)
- Rounding and displaying floats (C)
- applet wont refresh (Java)
- round off number (C)
- Rounding floating points (C++)
- Rounding floating points (C)
- How can I get float value with 2 decimal point? (Java)
Other Threads in the C++ Forum
- Previous Thread: Help with Classes/Array Program
- Next Thread: "Debug Assertion Failed!" Error
| Thread Tools | Search this Thread |
api array based binary bitmap business c++ c/c++ char class classes code coding commentinghelp compile console conversion count decide delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph guess gui homeworkhelp homeworkhelper iamthwee ifpug ifstream incrementoperators infinite input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem proficiency program programming project python random read recursion reference rpg string strings temperature template templates test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






