在 C++ 中将浮点类型的数据转换为整数有哪些不同的技术?
#include <iostream>
using namespace std;
struct database {
int id, age;
float salary;
};
int main() {
struct database employee;
employee.id = 1;
employee.age = 23;
employee.salary = 45678.90;
/*
How can i print this value as an integer
(with out changing the salary data type in the declaration part) ?
*/
cout << endl << employee.id << endl << employee.
age << endl << employee.salary << endl;
return 0;
}
原文由 moala 发布,翻译遵循 CC BY-SA 4.0 许可协议
如果您不能使用浮点运算,这是将 IEEE 754 浮点数转换为 32 位整数的一种方法。它还具有缩放功能,可以在结果中包含更多数字。缩放器的有用值为 1、10 和 100。