如何检查输入的数字是双精度还是整数? (1是整数但1.0是双精度) [英] How do I check whether a number entered is double or integer? (1 is an integer but 1.0 is double)

查看:373
本文介绍了如何检查输入的数字是双精度还是整数? (1是整数但1.0是双精度)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查输入是Integer还是double。我的问题是,当我在1.00之间(这是两倍)时,我的结果是整数。我如何编写区分(1.00和1)的应用程序??



我尝试过:



I want to check if the input is Integer or double. My problem is that when I inter 1.00 (which is double) my result is integer. How I can write app that distinguish between ( 1.00 and 1 ) ??

What I have tried:

int main()
{

double a;
cin >> a;
if ( floor(a) == ceil(a) )
	cout << "Integer";
else
	cout << "Double";

    return 0;
}

推荐答案

检查字符串 Luke,检查字符串

(搜索点)



您也可以使用 std :: strtol [ ^ ]提供(和检查) str_end 参数。
Check the string Luke, check the string!
(search for the dot)

You might also use std::strtol[^] providing (and the checking) the str_end parameter.


告诉的唯一方法是阅读它作为字符串而不是double,并手动解析。一旦你把它读成双,就无法告诉用户实际输入了什么。
The only way to tell is to read it as a string instead of a double, and manually parse that. Once you have read it into a double, there is no way to tell what the user actually entered.


你可以使用 sscanf 来将值设为字符串,然后计算字符串。谷歌是你的朋友。
You can use sscanf to make the value a string, and then evaluate the string. Google is your friend.


这篇关于如何检查输入的数字是双精度还是整数? (1是整数但1.0是双精度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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