如何用0代替VB asp替换nan. [英] How to replace nan with 0 instead VB asp.

查看:278
本文介绍了如何用0代替VB asp替换nan.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我试图在这里进行计算.但是当我输入0时,其值为Nan?如何将其替换为0.

我尝试过的事情:

hello guys im trying to make calculation here . but when i input 0 its values is Nan ? how to replace it by 0 instead.

What I have tried:

totalms = Val(wwms1.Text) + Val(wwms2.Text) + Val(wwms3.Text) + Val(wwms4.Text) + Val(wwms5.Text) + Val(wwms6.Text) + Val(wwms7.Text) + Val(wwms8.Text) + Val(wwms9.Text) + Val(wwms10.Text)
            totalrs = Val(wwrs1.Text) + Val(wwrs2.Text) + Val(wwrs3.Text) + Val(wwrs4.Text) + Val(wwrs5.Text) + Val(wwrs6.Text) + Val(wwrs7.Text) + Val(wwrs8.Text) + Val(wwrs9.Text) + Val(wwrs10.Text)

推荐答案

使用 ^ ]方法.
请注意,您应该检查这种结果的原因,而不是盲目地将其替换为0.
Use the Double.IsNan[^] method.
Please note, you should check the reason of such a result, instead of blindly replace it with 0.


如果输入到控件中(wvms. .和wvrs ..是Textboxes吗?)没有可转换的数字值,因此您无法获得数字值.在这种情况下,您必须检查输入是否有效-对于每个单独的控件.
If the Input into your Control (wvms.. and wvrs.. are Textboxes ?) has not convertible numeric Value you can''t get a numeric Value. In this case you have to check if the Input a valid - for each sepearte Control.


好,使用Val函数 [类型转换函数 [ ^ ]
VB.NET:
Double.TryParse方法(字符串,NumberStyles,IFormatProvider,Double)(系统) [ ^ ]
Double.Parse方法(字符串)(系统) [ ^ ]

用伪代码:

Well, using Val function[^] is really bad practice. Please, follow the link to find out why.

I''d suggest to use a proper way to convert text into numbers. For example:
VB:
CDbl(), CLng(), etc., See: Type Conversion Functions[^]
VB.NET:
Double.TryParse Method (String, NumberStyles, IFormatProvider, Double) (System)[^]
Double.Parse Method (String) (System)[^]

In pseudo code:


Dim tot As Double, tmp As Double

On Error GoTo Err_Txt2Dbl

For Each obj As TextBox In Form1.Controls
    If obj.Name Like "wwms*" Then
        tmp = 0
        'if conversion is possible, calculate total
        tmp = CDbl(obj.Text)
        tot += tmp
    End If
Next

Exit_MySubroutine:
    Exit Sub

Err_Txt2Dbl:
    Select Case Err.Number
        Case 6 'overflow
            tmp = 0
            Resume Next
    End Select





Dim st As NumberStyles = NumberStyles.Number Or NumberStyles.AllowCurrencySymbol
Dim ci As CultureInfo = CultureInfo.CreateSpecificCulture("en-us")
Dim tot As Double = 0
Dim tmp As Double = 0
For Each obj As TextBox In Form1.Controls
    If obj.Name.StartsWith("wwms") Then
        tmp = 0
        'if conversion is possible, calculate total
        If Double.TryParse(control.Text, st, ci, tmp) Then
            tot += tmp
        End If
    End If
Next




有关更多详细信息,请参见:
关于错误声明(Visual Basic) [ Visual Basic .NET中的错误处理 [



For further details, please see:
On Error Statement (Visual Basic)[^]
Error Handling in Visual Basic .NET[^]


这篇关于如何用0代替VB asp替换nan.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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