开发者

How to convert decimal number into 64 bit binary float number?

I need to convert decimal number into 64 bit binary float value. If you k开发者_开发技巧now any algorithm or anything about it then please help.


Use boost::lexical_cast.

double num = lexical_cast<double>(decimal);


Assuming you mean a decimal stored inside a string, atof would be a standard solution to convert that to a double value (which is a 64-bit floating point number on the x86 architecture).

std::string s = "0.4";
double convertedValue = atof(s.c_str());

Or similar for C strings:

const char *s = "0.4";
double convertedValue = atof(s);

But if you mean integer number by "decimal number", then just write

int yourNumber = 100;
double convertedValue = yourNumber;

and the value will automatically be converted.


Value casting from a string to double can be implemented by boost::lexical_cast. Type casting from int to double is a part of C++:

double d = (double)i;

It was already mentioned in the previous replies.

If you are interested to know how this casting is implemented, you may refer the sources of the C standard library your compiler is using given that the sources are provided and no floating point co-processor is used for this purpose. Many embedded target compilers do this work "manually" if no floating point co-processor is available.

For the binary format description, please see Victor's reply


Decimal decimalNumber = 1234;
Float binaryFloatValue = decimalNumber;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜