将字符串转换为双精度 - VB [英] Convert String to Double - VB

查看:34
本文介绍了将字符串转换为双精度 - VB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VB中是否有一种有效的方法来检查字符串是否可以转换为double?

Is there an efficient method in VB to check if a string can be converted to a double?

我目前正在尝试将字符串转换为双精度值,然后查看它是否引发异常.但这似乎减慢了我的申请速度.

I'm currently doing this by trying to convert the string to a double and then seeing if it throws an exception. But this seems to be slowing down my application.

Try
    ' if number then format it.
    current = CDbl(x)
    current = Math.Round(current, d)
    Return current
Catch ex As System.InvalidCastException
    ' item is not a number, do not format... leave as a string
    Return x
End Try

推荐答案

如果您使用的是 .NET 1.1/2.0/3.0/3.5/4.0/4.5,请尝试查看 Double.TryParse()

Try looking at Double.TryParse() if you are using .NET 1.1/2.0/3.0/3.5/4.0/4.5

这篇关于将字符串转换为双精度 - VB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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