在 c++ 中将浮点转换为 int 时丢失数据
Losing data when casting float to int in c++
除了明显的分数值预期损失之外,我不明白为什么会发生这种情况......
考虑以下用于将数字转换为英语短语的家庭作业代码片段:
int main() {
float dollars;
cout << "Please supply a dollar amount between $0.01 and $9999.99 (without the dollar sign): ";
while (true) {
cin >> dollars;
if (cin.fail() || dollars < 0.01 || dollars > 9999.99) {
cin.clear();
cin.ignore();
cout << "You provided an invalid number. Please try again: ";
} else {
// Test code:
cout << "You entered: " << dollars << endl;
cout << "Times 100 without cast: " << dollars * 100 << endl;
cout << "Times 100 with cast: " << (int)(dollars * 100) << endl;
cout << "Cents: " << ((int)(dollars * 100)) % 100 << endl;
break;
}
}
printDollarsAsString(dollars);
return 0;
}
我注意到,在提供值 9999.21
时,else
块中第二个和第三个 cout
语句的输出相差 1。例如,这是我运行此代码时的输出:
Please supply a dollar amount between $0.01 and $9999.99 (without the dollar sign): 9999.21
You entered: 9999.21
Times 100 without cast: 999921
Times 100 with cast: 999920
Cents: 20
如何解决此问题以正确检索美分值?我可以采取不同的方法吗?
cout << "Times 100 without cast: " << dollars * 100 << endl;
cout << "Times 100 with cast: " << (int)(dollars * 100) << endl;
当您转换为 int 时,您正在截断数字,而不是四舍五入。在这种情况下,.21
不能完全由二进制浮点数来扭曲,因为它实际上是一个重复的数字。重复数略小于 .21
。因此,当您乘以 100 时,您会得到999920.9...
,该截断以与演员表999920
。
这应该可以解决它:
cout << "Times 100 without cast: " << dollars * 100 << endl;
cout << "Times 100 with cast: " << (int)(dollars * 100 + 0.5) << endl;
这是因为四舍五入。
完整代码:
} else {
// Test code:
cout << "You entered: " << dollars << endl;
cout << "Times 100 without cast: " << dollars * 100 << endl;
cout << "Times 100 with cast: " << (int)(dollars * 100 + 0.5) << endl;
cout << "Cents: " << ((int)(dollars * 100 + 0.5)) % 100 << endl;
break;
}
或者:
} else {
// Test code:
cout << "You entered: " << dollars << endl;
cout << "Times 100 without cast: " << dollars * 100 << endl;
cout << "Times 100 with cast: " << (int)round(dollars * 100) << endl;
cout << "Cents: " << ((int)round(dollars * 100)) % 100 << endl;
break;
}
只是因为你问了不同的方法:)
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
...
std::string str_dollars;
char delim = '.';
std::vector<std::string> elems;
std::cin >> str_dollars;
std::stringstream ss(str_dollars);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
cout << "Cents: " << elems[1] << endl;
我最终在 round() 的帮助下解决了它,以便在 C++ 中为 float
。这也允许我接受小数美分,我可以相应地四舍五入。以下输出我所期望的:
cout << "Times 100 without cast: " << round(dollars * 100) << endl;
cout << "Times 100 with cast: " << (int) round(dollars * 100) << endl;
cout << "Cents: " << (int) round(dollars * 100) % 100 << endl;
相关文章:
- int数据类型的指针指向的是什么,如果是一个类的私有数据成员,我们创建了该类的两个对象?
- 如何将 std::vector 索引交换到<int>数据,将数据交换到索引
- 返回int数据类型v/s传递参考
- 如何让我的构造函数和函数工作,以便我的 main() 能够同时显示字符串和 int 数据
- 使用iStream从文件中读取int数据
- 直观地看到为什么操作">>1"并不意味着"/2" C++中的 int 数据
- 限制 int 数据类型的大小
- 由INT数据类型(低,高)结构表示的双数据类型
- 当我们给char数据类型赋值为负值时,它为什么表现得像int数据类型
- 您能否安全地将 char 数据存储在 int 数据类型中
- GCC 默认 int 数据成员初始化
- 为什么使用"int"数据类型而不是"float"数据类型?
- 为什么在visual studio中连续的int数据类型变量位于12个字节的偏移
- 使用'int'数据类型时出错,即使数字不是小数
- 在64位机器上使用8字节作为int数据类型大小的任何C/ c++编译器
- 为什么对于 long int 数据类型,左位移位<<移位不会超过 31?
- 为什么有人会使用int数据类型
- 将 Object int 数据成员转换为浮点数并划分会将奇怪的数据 cout 附加到控制台
- char* 和 int* 数据类型有什么区别,可以在C++中创建指针数组
- 在c++中保存Int数据到文本文件