### set precision without std::cout

Hello! I have a floating point number from which I have to output only the decimals. The problem is that my teacher wants us to output 20 exact decimals from that number, separated by a space. I thought about using `setprecision(20)`, but I don't know how to use it in other way than with `std::cout` .

 ``1234567`` `````` for(i=0;i

I'm using files for I/O

Last edited on
You could use a std::stringstream which is used the same way as std::cout but instead of getting the output printed on the screen you would get it as a string.
Last edited on
Hello flav32,

I am only sure of two ways to set the precision and that is `std::cout << std::setprecision(20)` or `std::cout.precision(20)`. Since "setprecision" is a stream manipulator it will be be the easiest way to show 20 decimal places.

An alternative would be to put the number into a string and pad the right side of the number with zeros before you print it to the screen.

And if you are not familiar with the "modf()" function from "cmath" header file you can read up on it here http://www.cplusplus.com/reference/cmath/modf/

Hope that helps,

Andy
 ``123456`` ``````for(i=1;i<=n;i++) { L=a[i]/b[i]; Lt=Lt+L;} fractpart= modf(Lt,&intpart); fout<

What am I doing wrong? for This input:
n=1
a[i]=7
b[i]=5, I should get as output 4 0 0 0 0 0 ....(20 times), but instead I just get 0..

 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````#include void print_div_result( unsigned int numerator, unsigned int denominator, unsigned int precision = 20 ) { std::cout << numerator << '/' << denominator << " == " ; if( denominator == 0 ) std::cout << ( numerator == 0 ? "NaN" : "+infinity" ) << '\n' ; else { // compute the quotient and remainder const unsigned int quotient = numerator / denominator ; unsigned int remainder = numerator % denominator ; std::cout << quotient << '.' ; for( unsigned int i = 0 ; i < precision ; ++i ) { remainder *= 10 ; std::cout << remainder / denominator ; remainder %= denominator ; } std::cout << "\ntruncated to " << precision << " digits after the decimal point\n\n" ; } } int main() { print_div_result( 22, 7 ) ; print_div_result( 355, 113 ) ; print_div_result( 22, 7, 5 ) ; print_div_result( 355, 113, 9 ) ; print_div_result( 2, 7, 60 ) ; }``````

http://coliru.stacked-crooked.com/a/103de26fdd018d95
Here is your code with formatting that reflects the block structure
 ``123456`` ``````for(i=1;i<=n;i++) { L=a[i]/b[i]; Lt=Lt+L; } fractpart= modf(Lt,&intpart); fout<

This will only output one number. Even if you move the closing brace to the end it won't work. Why are you adding L to Lt each time through the loop? Why are you recomputing L each time through the loop? Why are you printing the fractional part when you want to print individual digits (which suggests printing the integer part)?

My advice is to throw away this code and try to write this function again from scratch.
Mr jonathan is a plumber. n represents the number of streets he works on. Each street has one pipe that needs to be repaired. Mr jonathan wrote two numbers.Their ratio represents the lenght of the pipe.
We need to find the total lenght of all pipes (Lt) and output only the decimals of it.

EXAMPLE:
1 street
7 , 5 the sizes of the pipe
This means 7/5=1,4, so we will have 4 0 0 0 0 0 0 ...(20 times) as output.(Notice that the decimals are separated by a space)
Pretty stupid assignment imo...
Look at JLBorges's solution.
Topic archived. No new replies allowed.