双重转换 [英] Double to Double Conversion

查看:100
本文介绍了双重转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

菜鸟问题.但是找不到任何东西.

我有一个变量说

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.

推荐答案

事实并非如此.我知道,它 看起来像 ,但相信我,它不是.

发生的是,当您显示该数字时,该数字已被四舍五入.尝试在两步和单步执行的第一条指令上设置断点,同时查看变量值.您会发现该分配也不起作用!它改为分配值"123.06999999999999999999" ...:laugh:
It doesn''t. I know, I know, it looks like it does, but trust me, it doesn''t.

What is happening is that the number is being rounded up when you display it. Try putting a breakpoint on teh first instruction of the two and single step through, looking at the variable values as you go. You will find that the assignment doesn''t work either! It assigns a value of "123.06999999999999999999" instead... :laugh:


我的编译器给出的是123.07,而不是123.1.您使用了哪个编译器?
My compiler gives 123.07, not 123.1. Which compiler did you used?
#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


您如何检查的?如果有四舍五入,则必须相同.
在我的系统上,代码
How did you check it? If there is a roundoff, it must be the same.
On my system, the code
#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.


这篇关于双重转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆