Fabien
Fabien

Reputation: 13456

GMP: how to write a number as a decimal value?

I have an mpq_class instance, and I want to display it as a decimal value, rather than a fraction.

mpq_class nb = 1.75;
cout << nb << endl;

Output:

7/4

How can I output 1.75 rather than 7/4?

Upvotes: 2

Views: 444

Answers (1)

ShadowRanger
ShadowRanger

Reputation: 155734

Easiest approach would probably be to convert to mpf_class, e.g. cout << mpf_class(nb) << endl;. You might need to explicitly set the output precision to avoid printing out garbage from floating point errors though.

Upvotes: 1

Related Questions