Split a 'Decimal' in VB.NET

后端 未结 3 1743
醉梦人生
醉梦人生 2020-12-20 12:07

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

相关标签:
3条回答
  • 2020-12-20 12:57

    Simply:

    DecimalNumber - Int(DecimalNumber)
    
    0 讨论(0)
  • 2020-12-20 13:03

    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
    
    0 讨论(0)
  • 2020-12-20 13:10

    (As Jon Skeet says), beware that the integer part of a decimal can be greater than an integer, but this function will get you the idea.

        Sub SlipDecimal(ByVal Number As Decimal, ByRef IntegerPart As Integer, _
                        ByRef DecimalPart As Decimal)
            IntegerPart = Int(Number)
            DecimalPart = Number - IntegerPart
        End Sub
    

    Use the Jon version if you are using big numbers.

    0 讨论(0)
提交回复
热议问题