简体   繁体   中英

Is it possible to return a double with scientific notation in C++?

I'm currently doing a code kata (this one: https://www.codewars.com/kata/evaluate-mathematical-expression/train/cpp ) and in one of the random tests, it states:

Expected: equal to -1286.49 (+/- 1e-06)

Actual: -1286.49

It seems like the kata wants me to include scientific notation. However, the return type can only be a double, not a string.

Is there any way to do so with a double return type in C++? Thanks.

It is possible to use scientific notation when converting a floating point number to a character string. In case of a character stream (such as the standard output stream), the std::scientific I/O manipulator can be used.

A floating point number itself doesn't contain a textual representation, and it is not possible to return a textual representation from a function that returns a floating point number.

Instead of returning a double that uses scientific notation, rather print them as scientific notations. You'll need to use the std::scientific manipulator defined in <iomanip> .

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