print up to n decimals

G

Gary Wessle

Hi

in C++, is there a way to print out the float value up to a given
decimal places?
say 1/3 to .3333333 "7 decimals"

thanks
 
V

Victor Bazarov

Gary said:
in C++, is there a way to print out the float value up to a given
decimal places?
say 1/3 to .3333333 "7 decimals"

Yes. See 'fixed', 'setprecision' and other I/O manipulators (the
header <iomanip>).

V
 
T

Thomas Tutone

Gary said:
in C++, is there a way to print out the float value up to a given
decimal places?
Yes.

say 1/3 to .3333333 "7 decimals"

Look up stream manipulators (in header <iomanip>) in your favorite C++
reference book.

Best regards,

Tom
 
M

Mike Wahler

Gary Wessle said:
Hi

in C++, is there a way to print out the float value up to a given
decimal places?
say 1/3 to .3333333 "7 decimals"

#include <ios>
#include <iomanip>
#include <iostream>

int main()
{
double d(0);
d = 1.0 / 3.0;

std::cout << std::fixed << std::setprecision(7)
<< d << '\n';

return 0;
}

Output:

0.3333333

-Mike
 
V

Victor Bazarov

Mike said:
Gary Wessle said:
Hi

in C++, is there a way to print out the float value up to a given
decimal places?
say 1/3 to .3333333 "7 decimals"

#include <ios>
#include <iomanip>
[..]

Give a man a fish... Good if he can figure out how you caught it...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top