简体   繁体   English

如何将双精度转换为字符串的总位数(小数点的左侧和右侧)?

[英]How to convert double to string with precision for total number of digits(both left and right side of the decimal)?

I am looking for a way to convert double to string in c++ such that total number of digits remain to be 10 irrespective of how many are present before and after the decimal point and irrespective of the zeroes.我正在寻找一种在 c++ 中将双精度转换为字符串的方法,这样无论小数点前后有多少位和零位,总位数都保持为 10。 Examples for better understanding:更好理解的例子:

0.00000000000000000000 =expected outcome> 0.0000000000 0.00000000000000000000 =预期结果> 0.0000000000

12345.00000000000000000 =expected outcome> 12345.00000 12345.00000000000000000 =预期结果> 12345.00000

-15.123456789012 =expected outcome> -15.12345678 -15.123456789012 =预期结果> -15.12345678

I couldnt find any relevant answer.我找不到任何相关的答案。 For the methods like snprintf, std::setprecision with ostringstream, to_string, Boost's lexical_cast, some of the above case fails.对于 snprintf、std::setprecision 和 ostringstream、to_string、Boost 的 lexical_cast 等方法,上述某些情况会失败。

Example of code:代码示例:

double num = 12345.0000000000001;

std::ostringstream streamObj2;

streamObj2 << std::fixed <<  std::setprecision(10) << num;

std::string strObj2 = streamObj2.str();

std::cout  << strObj2 << '\n';

The output = 12345.0000000000, which is not what I am expecting. output = 12345.0000000000,这不是我所期望的。 Removing std::fixed gives output as 12345删除 std::fixed 给 output 为 12345

What I require = 12345.00000我需要的 = 12345.00000

Please help me, thanks.请帮助我,谢谢。

Thanks for the comments, because of which I found the answer.感谢您的评论,因此我找到了答案。 Using std::showpoint instead of std::fixed helped fulfill the requirement.使用 std::showpoint 而不是 std::fixed 有助于满足要求。

double num = 12345.0000000000001;双数 = 12345.0000000000001;

std::ostringstream streamObj2; std::ostringstream 流Obj2;

streamObj2 << std::showpoint << std::setprecision(10) << num; streamObj2 << std::showpoint << std::setprecision(10) << num;

std::string strObj2 = streamObj2.str(); std::string strObj2 = streamObj2.str();

std::cout << strObj2 << '\n'; std::cout << strObj2 << '\n';

Output: 12345.00000 Output: 12345.00000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将浮点数转换为具有指定精度和十进制位数的字符串? - Convert float to string with precision & number of decimal digits specified? 如何在左右两侧创建运算符*(double)以进行相乘? - How to create operator*(double) for multiplying on both the left and right hand side? 如何为小数点分隔符和位数调整 std::stod(字符串加倍) - How to tweak std::stod (string to double) for decimal separator and number of digits 如何在C ++中将字符串转换为具有指定精度的双精度数 - How to convert string to double with specified number of precision in c++ 将double转换为字符串,以保持数字的高精度,C ++ - Convert double to string keeping high precision of the digits, C++ 32位十进制数的浮点/双精度分析 - Analysis of float/double precision in 32 decimal digits 将double转换为具有超过5位十进制数字精度的字符串 - Convert double to string with more than 5 decimal digit precision 双十进制数字转换为十六进制字符串 - Double decimal number convert to HEX string C ++如何将双精度型转换为字符串,使其仅在小数点后显示2位数字? - C++ How to convert a double to a string so it only displays 2 digits after the decimal? 如何将字符串转换为给定精度的双精度? - How to convert a string to given precision double?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM