验证浮点数 [英] verify float number

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

问题描述

嘿大家好我需要帮助。我需要验证用户输入的数字是否为负数(-100.00)或

字母(a,b,c,X, Y)以及除正数
整数或小数点以外的其他数字。例如:


输入金额:


并捕获浮动变量,如下所示:


scanf("%f"& myVar)


我使用scanf来捕获数据,但是我很难过

用isdigit或isalpha验证此浮点数。任何想法都会很高兴。


谢谢

解决方案

Xancatal写道:


大家好。我需要帮助。我需要验证用户输入的数字是否为负数(-100.00)或

字母(a,b,c,X, Y)以及除正数
整数或小数点以外的其他数字。例如:


输入金额:


并捕获浮动变量,如下所示:


scanf("%f"& myVar)


我使用scanf来捕获数据,但是我很难过

用isdigit或isalpha验证此浮点数。任何想法将是

非常感谢。


谢谢



对不起;它不会那样工作。 C是一种低级语言。

如果你使用scanf那么它会尽力读入并为你做转换

(一旦得到它就停止有点不合适)

如果你真的想做低级别检查,你需要读取

字符串作为字符串,然后解析它你自己。

然而你可以测试一下这个数字是否为负数。

为什么你不是只读整数?
< br $>
-

Bill Medland


Xancatal写道:


>

嘿大家好。我需要帮助。我需要验证用户输入的数字是否为负数(-100.00)或

字母(a,b,c,X, Y)以及除正数
整数或小数点以外的其他数字。例如:


输入金额:


并捕获浮动变量,如下所示:


scanf("%f"& myVar)


我使用scanf来捕获数据,但是我很难过

用isdigit或isalpha验证此浮点数。任何想法将是

非常感谢。



if((1!= scanf(%f,& myvar))||(myvar< 0)){

/ *处理错误输入* /

}


始终检查输入例程调用是否有错误。


-

Chuck F(cinefalconer at maineline dot net)

可用于咨询/临时嵌入式和系统。

< http://cffalconer。 home.att.net>


感谢您的回复Bill。你的意思是读为整数然后

解析它?把它转换成浮动怎么样?那可能是
吗?你有可能的例子吗?


我想我可以使用getchar从

float(或用户)中捕获流,做验证(你知道......不奇怪

字符,如


Hey everybody. I need help on this one. I need to verify that a number
entered by a user is not either a negative number (-100.00), or an
alphabet (a, b, c, X, Y) as well as other number other than positive
integers or a decimal point. For example:

Enter amount:

and was capturing the float varialbe as in:

scanf ("%f", &myVar)

I was using scanf to capture the data, but I''m having a hard time
verifying this float with isdigit or isalpha. Any ideas would be
greatly appreciated.

Thanks

解决方案

Xancatal wrote:

Hey everybody. I need help on this one. I need to verify that a number
entered by a user is not either a negative number (-100.00), or an
alphabet (a, b, c, X, Y) as well as other number other than positive
integers or a decimal point. For example:

Enter amount:

and was capturing the float varialbe as in:

scanf ("%f", &myVar)

I was using scanf to capture the data, but I''m having a hard time
verifying this float with isdigit or isalpha. Any ideas would be
greatly appreciated.

Thanks

Sorry; it doesn''t work that way. C is a lower level language than that.
If you use scanf then it will do its best to read in and do the conversion
for you (and stop once it gets to a bit that doesn''t fit)
If you really want to do low level checking you will need to read in the
string as a string and then parse it yourself.
However you can test to see if the number is negative.
Why don''t you just read the number in as an integer?

--
Bill Medland


Xancatal wrote:

>
Hey everybody. I need help on this one. I need to verify that a number
entered by a user is not either a negative number (-100.00), or an
alphabet (a, b, c, X, Y) as well as other number other than positive
integers or a decimal point. For example:

Enter amount:

and was capturing the float varialbe as in:

scanf ("%f", &myVar)

I was using scanf to capture the data, but I''m having a hard time
verifying this float with isdigit or isalpha. Any ideas would be
greatly appreciated.

if ((1 != scanf(%f, &myvar)) || (myvar < 0)) {
/* handle bad entry */
}

Always check input routine calls for errors.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>


Thanks for your response Bill. Do you mean read as integer and then
parse it? How about converting it then to float? would that be
possible? Do you have maybe an example I can go by?

I wast thinking I could use getchar to capture the stream from the
float (or the user), do the verification (you know... No weird
characters like


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

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