问题描述
菜鸟问题.但是找不到任何东西.
我有一个变量说
Noob question. But could not find anything.
I have a variable say
double a= 123.07
当我使用
when I use a
double b = a;
它将a的值分配为123.1而不是123.07
将一个double值分配给另一个double变量时,如何删除此舍入.
关于这个值,我正在做的是,将文件解析为令牌,这些令牌之一就是变量a的值.
打破后,将令牌的值分配给一个字符串,然后使用
分配给一个浮点数
double a = atof(amount.c_str());
现在,我将此值发送到我创建的数据结构Class中.
在类构造函数中,我通过此值初始化一个双精度类型的类变量.
类定义:
sortTran(字符串trantype,字符串ts,字符串buf,字符串数量,双bal,长ts2)
现在,当我使用object-> varname调用类成员时,只不过是double的相同值而已,
我得到四舍五入的价值.
我试图在调用函数中创建一个局部变量,甚至四舍五入.
我很沮丧!我找不到任何东西,因为我的项目的输出文件的二进制代码与所需的输出二进制文件匹配.甚至空格键也可以帮我解决问题.
it assign value of a as 123.1 instead of 123.07
How can I remove this rounding off while assigning a double value to another double variable.
About this value what i am doing is , parsing a file into tokens, one of these tokens being the value of variable a.
after breaking the value of token is assigned to a string and then to a float using
double a= atof(amount.c_str());
Now, i am sending this value to a data structure Class created by me.
In the class constructor i initialize a double type class variable by this value.
Class definition :
sortTran(string trantype,string ts,string buf, string amount, double bal, long ts2)
Now when i call class member using object->varname which is nothing but the very same value of double,
i get the value rounded off.
i tried to create a local variable in the calling function and even that is rounding off.
I am stumped !! i could not find anything as my project''s output file''s binary code is matched with desired output''s binary. and even a space bar can screw up the complete thing for me.
推荐答案
#include<stdio.h>
int main()
{
double a= 123.07;
double b = a;
printf("%lf\n",b);
return 0;
}
</stdio.h>
它是123.07
It comes 123.07
#include<iostream>
using namespace std;
int main()
{
double a= 123.07;
double b = a;
cout << "a = " << a << ", b = " << b << endl;
}
给出以下输出:
gives the following output:
a = 123.07, b = 123.07
同时,使用调试器可以看到两个 a, b
的值为123.06999999999999
.
while, using the debugger I can see both a, b
having value 123.06999999999999
.
这篇关于双重转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!