C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Floating Point

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Bob Smith
Guest





PostPosted: Fri Dec 26, 2003 10:01 am    Post subject: Re: Floating Point Reply with quote



"geertjoos (AT) pandora (DOT) Be" wrote:

Quote:
Hi,

A pice of code.

float A = 10;
float B = 7;
float result;

result = A / B;

when i run this program, i've got as result 1,4285714.... etcetc.
is it possible to get only two or one decimals after the floating point? And how?

Thanks in advance.
Geert

round it
/B


Back to top
geertjoos@pandora.Be
Guest





PostPosted: Fri Dec 26, 2003 2:13 pm    Post subject: Floating Point Reply with quote



Hi,

A pice of code.

float A = 10;
float B = 7;
float result;

result = A / B;

when i run this program, i've got as result 1,4285714.... etcetc.
is it possible to get only two or one decimals after the floating point? And how?

Thanks in advance.
Geert
Back to top
Geert Joos
Guest





PostPosted: Fri Dec 26, 2003 3:25 pm    Post subject: Re: Floating Point Reply with quote




"Bob Smith" <bobsmith1 (AT) marketweighton (DOT) com> schreef in bericht
news:3FEC06DD.6833C938 (AT) marketweighton (DOT) com...
Quote:
"geertjoos (AT) pandora (DOT) Be" wrote:

Hi,

A pice of code.

float A = 10;
float B = 7;
float result;

result = A / B;

when i run this program, i've got as result 1,4285714.... etcetc.
is it possible to get only two or one decimals after the floating point?
And how?

Thanks in advance.
Geert

round it
/B
And how do you do that, can you give me an example? I'm just a bit new in

C++
Thanks.
Geert
Quote:




Back to top
Adam Fineman
Guest





PostPosted: Fri Dec 26, 2003 4:02 pm    Post subject: Re: Floating Point Reply with quote

Geert Joos wrote:
Quote:
"Bob Smith" <bobsmith1 (AT) marketweighton (DOT) com> schreef in bericht

round it
/B

And how do you do that, can you give me an example? I'm just a bit new in
C++

Here's one way:

#include <iostream>
#include <cmath>

using namespace std;

int
main()
{
float A = 10;
float B = 7;

float result = A / B;

float prec = 2.0;
float shift_factor = pow(10, prec);

cout << round(result * shift_factor) / shift_factor << 'n';

return 0;
}

Note that the above method is not particularly robust, elagent, or
efficient. It's probably not even accurate for arbitrary values of
'result'. Use at your own risk.

- Adam

--
Reverse domain name to reply.


Back to top
Alexandros
Guest





PostPosted: Fri Dec 26, 2003 5:16 pm    Post subject: Re: Floating Point Reply with quote

[email]geertjoos (AT) pandora (DOT) Be[/email] escribió:
Quote:
Hi,

A pice of code.

float A = 10;
float B = 7;
float result;

result = A / B;

when i run this program, i've got as result 1,4285714.... etcetc.
is it possible to get only two or one decimals after the floating point? And how?

Thanks in advance.
Geert

If what you want is to display the number with 2 decimals although it
actually has more you can do the following:


#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
float a=3.239487;
float b=23.234;
float r=b/a;

cout << setprecision(3) << r << endl;

return 0;
}



Back to top
Jeff Schwab
Guest





PostPosted: Fri Dec 26, 2003 5:33 pm    Post subject: Re: Floating Point Reply with quote

[email]geertjoos (AT) pandora (DOT) Be[/email] wrote:
Quote:
Hi,

A pice of code.

float A = 10;
float B = 7;
float result;

result = A / B;

when i run this program, i've got as result 1,4285714.... etcetc.
is it possible to get only two or one decimals after the floating point? And how?

Thanks in advance.
Geert



#include <iostream>

/* Truncate a number to (at most) two digits past the decimal point.
* NB: This implementation is naive.
*/
template< typename T >
T round2( T const& t )
{
return int( t * 100 ) / 100.0;
}

int main( )
{
float A = 10;
float B = 7;
float result;

result = A / B;

std::cout << result << 'n';
std::cout << round2( result ) << 'n';
}


Back to top
Walter
Guest





PostPosted: Sat Dec 27, 2003 9:14 am    Post subject: Re: Floating Point Reply with quote


"geertjoos (AT) pandora (DOT) Be" <geertjoos (AT) Pandora (DOT) Be> wrote

Quote:
float A = 10;
float B = 7;
float result;

result = A / B;

when i run this program, i've got as result 1,4285714.... etcetc.
is it possible to get only two or one decimals after the floating point?
And how?


float A = 10;
float B = 7;
float result;

result = ((long)((A * 100) / B)) / 100.0;

-Walter
www.digitalmars.com free C/C++/D compilers



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.