双到字符串用指数表示的大数字的转换
Double to String Conversion of large numbers with exponential
我有一个应用程序,其中我必须处理非常大的数字。应用程序API给了我编号,然后我不得不将其转换为字符串并发送到进一步处理。代码接收号码为-153562179.1619387
然后对于转换,我使用了-
char buffer[255];
sprintf(buffer, "%e", OverallVolume); //OverallVolume has the above value.
现在,缓冲区变量返回1.535625e+009,标准化为1535625000.00但我的应用程序显示值为1.5356252e+09,标准化为1535625200.00
所以我想问我应该使用什么方法将Double转换为String,这样我得到的值将与应用程序显示的值相匹配。
如果我正确理解你的问题,你希望小数点后出现7位数字。为此,指定精度如下:
sprintf(buffer, "%.7e", OverallVolume); // buffer contains 1.5356252e+09
现场演示
此外,由于它被标记为C++,因此这里有一个使用IO流的版本,可以打印相同的结果。
#include <ios>
#include <iomanip>
#include <iostream>
#include <sstream>
int main()
{
double OverallVolume = 1535625179.1619387;
std::ostringstream ss;
ss << std::scientific << std::setprecision(7) << OverallVolume;
std::cout << ss.str() << 'n'; // prints 1.5356252e+09
}
现场演示
相关文章:
- 将数字转换为字母(例如:123 转换为一二三)
- 将数字转换为单词
- 使用if-else将数字转换为单词
- 将数字转换为填充字符串
- 编译时间文本到数字转换 (atoi)
- 在 c++ 中将数字转换为矢量数组的函数
- 奇怪的数字转换C++
- 用C++将浮点数字转换为本地化字符串
- 如何将ASCII数字转换为从文件中提取的整数
- 检查字符串是否是数字,然后将该数字转换为 int?
- 将 MPFR 数字转换为字符串并返回
- 如何将数字转换为整数向量?
- 如何在C++中将数字转换为链表
- 如何将字符串中的多个数字转换为整数
- 有问题将数字转换为罗马数字
- 这个给定的代码应该将给定的数字转换为尽可能滞后的数字,no.by 用 9.It 替换合适的数字是行不通的
- 将两位数字转换为低内存表示的最快方法
- 视觉 如何将混合的字母和数字转换为仅字母?C++
- 如何在 c++ 中将十六进制数字转换为字符
- 如何将字符串数字转换为各个数字的整数数组