如何将小数部分保留在答案中 [英] How to keep the decimal part in an answer

查看:71
本文介绍了如何将小数部分保留在答案中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

This is my method(vb.net): 
Public Sub avg(num1, num2)
        res1 = (num1 + +num2) \ 2
        MessageBox.Show("Average: " & res1)
    End Sub



res1是将数字显示为整数,我怎样才能显示结果数并保留小数部分?

Ps这个方法是从另一个类调用的,所以数字是从那里得到的,但结果仍然相同。



这是整个班级:


The res1 is displaying the number as integer,how can i get it to display the resulted number and keeping the decimal part?
P.s this method is being called from another class so the numbers are given from there but still same result.

This is the whole Class:

Public Class Mathexp
    Private num1 As Integer
    Private num2 As Integer
    Private res1 As Double


    Public Sub max(num1, num2)
        If (num1 > num2) Then
            MessageBox.Show("Max: " & num1)
        Else : MessageBox.Show("Max: " & num2)
        End If

    End Sub
    Public Sub min(num1, num2)
        If (num1 < num2) Then
            MessageBox.Show("Min: " & num1)
        Else : MessageBox.Show("Min: " & num2)
        End If

    End Sub
    Public Sub avg(num1, num2)
        res1 = (num1 + +num2) \ 2
        MessageBox.Show("Average: " & res1)
    End Sub

End Class





我尝试了什么:



我尝试添加'Cdec'和'Cdbl',但平均值仍显示为整数。

我以前在c语言中放置这样的东西(res =(float)num1 + num2)并且它工作但我是vb.net的新手



What I have tried:

I tried adding 'Cdec' and 'Cdbl' but still the average is being displayed as Integer.
I used to place something like this in c language (res=(float)num1+num2) and it worked but i am new to vb.net

推荐答案

通过将'\'切换为'/'找到解决方案,因为第一个是整数并返回int数字!
Found the solution by swithching '\' to '/' as the first one is for integers and returns int number!


即使有十进制除法,

Even with decimal division,
res1 = (num1 + +num2) / 2



仍然是错误的



你应该仔细研究VB操作员。

Visual Basic中的运算符和表达式 [< a href =https://msdn.microsoft.com/en-us/library/a1w3te48.aspxtarget =_ blanktitle =New Window> ^ ]

VB.Net - 运营商 [ ^ ]


这篇关于如何将小数部分保留在答案中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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