怎么判断Textbox文本框输入的值是否可以转换为Double类型呢,并且要求小数位只保留2位

MeBoss 2011-06-07 10:15:15
如题,小弟实在不会,谢谢啦
...全文
309 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
我是小谢 2011-11-20
  • 打赏
  • 举报
回复
我用C#编程,怎样判断文本框中输入的只能事double类型的数据啊,如果不是就让它提示信息出错
啊的发哦 2011-06-07
  • 打赏
  • 举报
回复
代码一个比一个长。。。。

自己写一个 IsDouble 的函数,函数里直接赋值给一个 double 类型的变量,出错就 false 。
MeBoss 2011-06-07
  • 打赏
  • 举报
回复
Private Function TextIsDouble(ByRef X As Double, txtName As TextBox) As Boolean
Dim i As Byte
Dim cur As Currency
'txtName.text是文本框的值
If IsNumeric(txtName.Text) = True Then
i = InStr(1, txtName.Text, ".", vbTextCompare)
If i > 0 Then
X = Val(Left(txtName.Text, i + 2))
TextIsDouble = True
Else
X = Val(txtName.Text)
TextIsDouble = True
End If
Else
TextIsDouble = False
End If
End Function

这是我想到的办法 但是不能四舍五入
booksoon 2011-06-07
  • 打赏
  • 举报
回复


private function TextIsLong() as boolean
dim cur as currency
'Text1.text是文本框的值
if isnumeric(text1.text)=true then
if instr(1,text1.Text ,".",vbTextCompare )<>0 then
TextIsLong=false
else
if len(text1.text)>11 then
TextIsLong=false
else
cur = val(text1.text)
if cur<-2,147,483,648 or cur > 2,147,483,647 then
TextIsLong=false
else
TextIsLong=True
end if
end if
end if
else
TextIsLong=false
end if
end function


饮水需思源 2011-06-07
  • 打赏
  • 举报
回复
private sub textbox1_lostfocus()
if trim(textbox1.text)<>"" then
if not isnumeric(textbox1.text) then
msgbox "请输入数值型数据!",48,"提示"
textbox1.setfocus
else
textbox1.text=format(textbox1.text,"###0.00")
end if
end if
end sub
flfq 2011-06-07
  • 打赏
  • 举报
回复
我也不会,等楼下的
贝隆 2011-06-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 leftie 的回复:]
VB code
private sub textbox1_lostfocus()
if trim(textbox1.text)<>"" then
if not isnumeric(textbox1.text) then
msgbox "请输入数值型数据!",48,"提示"
textbox1.setfocus
……
[/Quote]
+1

7,764

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧