Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output fraction instead of decimal number?

Tags:

c++

fractions

In C++, When I calculate 2/3, it will output decimal values, how can I just get the original format (i.e.g 2/3) instead of 0.66666667

Thanks

like image 749
Sean Avatar asked Jan 27 '11 16:01

Sean


People also ask

How do you switch decimals to fractions?

Decimals can be written in fraction form. To convert a decimal to a fraction, place the decimal number over its place value. For example, in 0.6, the six is in the tenths place, so we place 6 over 10 to create the equivalent fraction, 6/10. If needed, simplify the fraction.


1 Answers

You can't. You would need to write a class dedicated to holding rational numbers (i.e. fractions). Or maybe just use the Boost Rational Number library.

like image 102
Oliver Charlesworth Avatar answered Oct 08 '22 02:10

Oliver Charlesworth