将负数转换为正数 [英] converting a negative number to a positive

查看:303
本文介绍了将负数转换为正数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想将负数转换为正数.
match.abs(dif)不起作用.
还有其他可能的方法吗?
当我按原样运行代码时,如果dif为负,则保持负.


Hi
I want to convert a negative number to a positive.
the match.abs(dif) does not work.
Is there any other possible way to do this?
When i run the code as is ,if the dif is negative,it stays negative.


Dim Total As Decimal = 0
               Dim currTotal As Decimal = Math.Round(dtreader(1), 2)
               Total = Math.Round(Decimal.Parse((dtreader(2) + dtreader(3)) + dtreader(4)), 2)
               Dim avg As Decimal = Math.Round(Total / 3, 2)
               Dim dif As Decimal = currTotal - Total

               Math.Abs(dif)

               Dim StatisticDiff As Decimal = Math.Round((dif / currTotal) * 100, 2)

               If currTotal > Total Then
                   MsgBox(currTotal & "    : increased by : " & StatisticDiff)
               ElseIf currTotal < Total Then
                   MsgBox(currTotal & "    : decreased by : " & StatisticDiff)
               Else
                   MsgBox(currTotal & "    : stayed the same : " & StatisticDiff)
               End If

推荐答案

这很正常,您应该将Abs线修改为:
That is quite normal, you should modify your abs line to:
dif = Math.Abs(dif)


这样做,您将把dif的绝对值放入dif.

请参见此链接 [


Doing that you''ll put the absolute value of dif into dif.

See this link[^].

The Math.Abs function returns the absolute value of a number passed as parameter.
In that way you can do plenty of things:

dif = Math.Abs(dif)
another_var = Math.Abs(dif)
var3 = Math.Abs(function_call())
...


祝您好运!


Good luck!


Maths.Abs返回一个小数,它没有设置所提供参数的值.

因此,dif = Math.Abs(dif) 应该可以解决您的问题.
Maths.Abs returns a decimal, it doesn''t set the value of the supplied parameter.

So, dif = Math.Abs(dif) should fix your problem.


Math.Abs

方法 ^ ]返回包含结果的绝对值.
您需要捕获返回值.

Method[^] returns an absolute value that contains the result.
You need to capture the return value.


这篇关于将负数转换为正数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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