C ++浮点到整数类型转换 [英] C++ floating point to integer type conversions

查看:137
本文介绍了C ++浮点到整数类型转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,用于将浮点数据类型转换为整数的不同技术有哪些?

What are the different techniques used to convert float type of data to integer in 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;
}


推荐答案

/ p>

Normal way is to:

float f = 3.4;
int n = static_cast<int>(f);

这篇关于C ++浮点到整数类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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