By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,176 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

floating point precision

P: n/a
Is there a convenient function for changing the precision of a float?

(I have a bunch of floats that are currently printed out to 9 decimal
places and I'd like them to printed out at about 4)
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"TheFerryman" <fe***@onthenet.com> wrote in message
news:tv********************************@4ax.com...
Is there a convenient function for changing the precision of a float?

(I have a bunch of floats that are currently printed out to 9 decimal
places and I'd like them to printed out at about 4)


There is no way to change the precision of the float type itself, but you
can control the number of digits being printed. For example if you use
std::cout:

#include <iostream>
#include <iomanip>
int main()
{
float f = 1.0f/3.0f;
std::cout << std::setprecision(4) << f << "\n";
return 0;
}

Or if you prefer using the C way:

#include <stdio.h>
int main()
{
float f = 1.0f/3.0f;
printf("%4.4f\n", f);
return 0;
}

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl

Jul 19 '05 #2

P: n/a
On Sun, 10 Aug 2003 21:11:15 +0200, "Peter van Merkerk"
<me*****@deadspam.com> wrote:
"TheFerryman" <fe***@onthenet.com> wrote in message
news:tv********************************@4ax.com.. .
Is there a convenient function for changing the precision of a float?

(I have a bunch of floats that are currently printed out to 9 decimal
places and I'd like them to printed out at about 4)


There is no way to change the precision of the float type itself, but you
can control the number of digits being printed. For example if you use
std::cout:

#include <iostream>
#include <iomanip>
int main()
{
float f = 1.0f/3.0f;
std::cout << std::setprecision(4) << f << "\n";
return 0;
}

Or if you prefer using the C way:

#include <stdio.h>
int main()
{
float f = 1.0f/3.0f;
printf("%4.4f\n", f);
return 0;
}

that'll do fine thanks
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.