C ++:如何转换从浮点到字符串没有舍入,截断或填充? [英] C++: How to Convert From Float to String Without Rounding, Truncation or Padding?

查看:88
本文介绍了C ++:如何转换从浮点到字符串没有舍入,截断或填充?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到问题,无法解决问题。需要帮助从古茹。下面是示例代码: -

I am facing a problem and unable to resolve it. Need help from gurus. Here is sample code:-

float f=0.01f;
printf("%f",f);

a。有什么办法,我们可以强制编译器为float类型的变量赋予相同的值?

a. Is there any way that we may force the compiler to assign same values to variable of float type?

b。我想将float转换为字符串/字符数组。如何才能将仅有且完全相同的值转换为字符串/字符数组。我想确保没有填充零,没有不必要的值被填充,没有像上面的例子中的数字改变。

b. I want to convert float to string/character array. How is it possible that only and only exactly same value be converted to string/character array. I want to make sure that no zeros are padded, no unwanted values are padded, no changes in digits as in above example.

推荐答案

除了非常少的值(例如0.25),使用基数2值不可能精确地表示一个十进制的十进制数。要得到你所需要的,你必须从float / double内置类型切换到某种十进制数字包。

It is impossible to accurately represent a base 10 decimal number using base 2 values, except for a very small number of values (such as 0.25). To get what you need, you have to switch from the float/double built-in types to some kind of decimal number package.

这篇关于C ++:如何转换从浮点到字符串没有舍入,截断或填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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