2

Let's say I have a

mpf_t number;

that equals 1.25. I want to print it with a given precision, even if there would be trailing zero's, for example

1.2500000

When I try to print it by using a mpf_get_str function, it prints in scientific (e) format,

mpf_out_str(stdout,10,7,number); //prints 0.125e1

When I use c++'s cout, as mpf_t has << overloaded,

cout << number << endl; //prints 1.25

and when I try to print it by calling setprecision(given precision) in cout, i still get 1.25

cout << setprecision(7) << number << endl; //prints 1.25

So, how can I print the number as I'd like it?

user905747
  • 613
  • 1
  • 9
  • 18

1 Answers1

2

You need to send std::fixed to std::cout.

#include <iostream>
#include <gmpxx.h>

int main(int argc,char* argv[])
{
  mpf_t mpft;

  mpf_init(mpft);
  mpf_set_d(mpft,1.25);

  std::cout.precision(7);
  std::cout << std::fixed;
  std::cout << mpft << std::endl;

  return 0;
}
DomCote
  • 86
  • 1
  • 3