简体   繁体   中英

How to print a periodic number WITHOUT rounding up to n decimals?

Let's say x = 2/3 and n = 10

then I would like to print: .6666666666 (10 sixes, because n is 10)

instead of .6666666667 <- I don't want that seven!!!!

How would you print in that way either in C or C++ ?

C:

double foo = 2.0 / 3.0;
printf("%.10f", floor(foo * pow(10, 10)) / pow(10, 10));

C++:

double foo = 2.0 / 3.0;
std::cout
    << std::fixed
    << std::setprecision(10)
    << std::floor(foo * std::pow(10.0, 10.0)) / std::pow(10.0, 10.0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM