如何在c sharp中不允许小数或浮点数? [英] how not to allow decimal or float numbers in c sharp?

查看:146
本文介绍了如何在c sharp中不允许小数或浮点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hii,



我的要求是..我不想允许任何浮动值或小数位值...只有没有小数...如果用户输入我想抛出异常

Hii,

my requirement is .. i dont want to allow any floating values or decimal places value ... only without decimal ... if user enters i want to throw an exception

推荐答案

解决方案1可能会因文化差异而失败。

尝试:

Solution 1 may fail on possible cultural differences.
Try:
int price = int.Parse(priceTextBox.Text, CultureInfo.CurrentCulture);



你从中获得数值。

抛出 FormatException if .Text 不是整数值。

使用适当的代替 CultureInfo.CurrentCulture 对于不同的文化,

例如 CultureInfo.InvariantCulture



如果您对解析失败有一些不同的错误处理:


You get the numeric value from this.
Throws a FormatException if the .Text is not an integer value.
Use whatever is appropriate instead of CultureInfo.CurrentCulture for different cultures,
e.g. CultureInfo.InvariantCulture

If you'd rather some different error handling for the parse failures:

int price;
if (!int.TryParse(priceTextBox.Text, NumberStyles.Integer, CultureInfo.CurrentCulture, out price))
{
  // Whatever handling for not a simple integer goes here.
}
// price has the value here


验证输入



Validate the input

if (priceTextBox.Text.Contains(".")) {
    throw  new Exception("Decimal is not allowed.");
}


这篇关于如何在c sharp中不允许小数或浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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