本文介绍了在VB.NET中拆分“十进制”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确信这是一个非常简单的问题,但是我是VB.NET的新手,所以我遇到了问题。



我有一个十进制变量,我需要将其拆分为两个单独的变量,一个包含整数部分,另一个包含小数部分。



例如,对于x = 12.34,您最终会得到ay = 12和az = 0.34。



解决方案

您可以执行此操作吗?

解决方案

使用,然后从原始内容中减去它。请注意,如果输入为小数(例如-1.5 => -1,-.5),这将给您两个部分都带来负值



编辑:这是一个Eduardo的代码版本始终使用十进制:

  Sub SplitDecimal(ByVal number as Decimal,ByRef wholePart As Decimal,_ 
ByRef分数部分为小数)
整体部分= Math.Truncate(数字)
分数部分=数字-整体部分
结束子


I am sure this is a very simple problem, but I am new to VB.NET, so I am having an issue with it.

I have a Decimal variable, and I need to split it into two separate variables, one containing the integer part, and one containing the fractional part.

For example, for x = 12.34 you would end up with a y = 12 and a z = 0.34.

Is there a nice built-in functions to do this or do I have to try and work it out manually?

解决方案

You can use Math.Truncate(decimal) and then subtract that from the original. Be aware that that will give you a negative value for both parts if the input is decimal (e.g. -1.5 => -1, -.5)

EDIT: Here's a version of Eduardo's code which uses decimal throughout:

Sub SplitDecimal(ByVal number As Decimal, ByRef wholePart As Decimal, _
                 ByRef fractionalPart As Decimal)
    wholePart = Math.Truncate(number)
    fractionalPart = number - wholePart
End Sub

这篇关于在VB.NET中拆分“十进制”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 17:53