读书人

如何将一个很大的数写进一个文件时不用

发布时间: 2013-02-19 11:11:40 作者: rapoo

怎么将一个很大的数写进一个文件时不用科学计数法
先看下我的代码

ofstream mcfile; //创建对象
mcfile.open(tfwpathAuto); //创建文件
mcfile << pixel_x << endl;
mcfile << pixel_y << endl;
mcfile.close();

tfwpathAuto是我新建文件的路径以及文件名,比如C:\\111.tfw;
pixel_x和pixel_y是我要写进去的数值,float型
假如pixel_x = 40545678,写进文件就变成了4.05456e+007,
现在我不想用科学计数法

求指教~~~ c ,科学计数法
[解决办法]
输出之前设置一下


mcfile << std::fixed;

[解决办法]

引用:
输出之前设置一下
C/C++ code?1mcfile << std::fixed;


楼上正解!

[解决办法]
string StringCast(int source)
{
std::stringstream ss;
ss << source;
return ss.str();
}
[解决办法]
引用:
引用:输出之前设置一下


C/C++ code
?



12

mcfile << std::fixed;

这样有逗号,也不行....
我想要的数就像40543984这样的数

不知道你在说什么,数字类型哪来的逗号啊。下面是个例子,你看看能不能变成你需要的吧。

#include <limits>
#include <iostream>

int main ()
{
float const a = std::numeric_limits<float>::max();
std::cout << std::fixed;
std::cout.precision(0);
std::cout << a << std::endl;
return 0;
}

[解决办法]
关键是float变量只能保存最长十进制有效数字6位啊!
参考include\float.h
...
#define DBL_DIG 15 /* # of decimal digits of precision */
#define DBL_EPSILON 2.2204460492503131e-016 /* smallest such that 1.0+DBL_EPSILON != 1.0 */
#define DBL_MANT_DIG 53 /* # of bits in mantissa */
#define DBL_MAX 1.7976931348623158e+308 /* max value */
#define DBL_MAX_10_EXP 308 /* max decimal exponent */
#define DBL_MAX_EXP 1024 /* max binary exponent */
#define DBL_MIN 2.2250738585072014e-308 /* min positive value */
#define DBL_MIN_10_EXP (-307) /* min decimal exponent */
#define DBL_MIN_EXP (-1021) /* min binary exponent */


#define _DBL_RADIX 2 /* exponent radix */
#define _DBL_ROUNDS 1 /* addition rounding: near */

#define FLT_DIG 6 /* # of decimal digits of precision */
#define FLT_EPSILON 1.192092896e-07F /* smallest such that 1.0+FLT_EPSILON != 1.0 */
#define FLT_GUARD 0
#define FLT_MANT_DIG 24 /* # of bits in mantissa */
#define FLT_MAX 3.402823466e+38F /* max value */
#define FLT_MAX_10_EXP 38 /* max decimal exponent */
#define FLT_MAX_EXP 128 /* max binary exponent */
#define FLT_MIN 1.175494351e-38F /* min positive value */
#define FLT_MIN_10_EXP (-37) /* min decimal exponent */
#define FLT_MIN_EXP (-125) /* min binary exponent */
#define FLT_NORMALIZE 0
#define FLT_RADIX 2 /* exponent radix */
#define FLT_ROUNDS 1 /* addition rounding: near */
...


[解决办法]
LZ什么环境?我的环境不用做任何处理就能“原样”输出。当然我把pixel_x声明成int类型了。
LZ的pixel_x什么类型呢?

# include <iostream>

using namespace std;

int main()
{
int pixel_x = 40545678;

cout << pixel_x << endl;

return 0;
}


[解决办法]
引用:
样有逗号,也不行....
我想要的数就像40543984这样的数


是不是逗号分隔跟locale有关。

#include <iostream>
#include <locale>
#include <string>


#include <iterator>

int main()
{
double n = 1234567.89;
std::cout.imbue(std::locale("de_DE"));
std::cout << "Direct conversion to string:\n"
<< std::to_string(n) << '\n'
<< "Output using a german locale:\n"
<< std::fixed << n << '\n'
<< "Output using an american locale:\n";
// use the facet directly
std::cout.imbue(std::locale("en_US.UTF-8"));
auto& f = std::use_facet<std::num_put<char>>(std::cout.getloc());
f.put(std::ostreambuf_iterator<char>(std::cout), std::cout, ' ', n);
std::cout << '\n';
}
/*
Direct conversion to string:
1234567.890000
Output using a german locale:
1.234.567,890000
Output using an american locale:
1,234,567.890000
*/

读书人网 >C++

热点推荐