Integer and decimal part in VB

Hello,
who can help me extract the integer part separately
and the decimal part separately from a floating number in a VB program,
thank you for your help.

7 answers

  • Votes
  • Date
  • Descending date
  1. Dec = (Value - Int(Value))
    Integer = Int(Value)

    here is the exact syntax with Value = floating point number
    1. Hello,

      This method generally works, but for very large numbers, it will not work
      Integer: Contains signed 32-bit (4-byte) integers whose value ranges from -2,147,483,648 to 2,147,483,647.

      If the number to be tested is outside these limits, another solution must be found (which I am currently looking for :S)
    2. thank you very much for your help
  2. val2=(val1-int(val1))"decimal"
    val3=int(val1)"integer part"
    syntax to check
    1. Three other solutions (but they work):

      Put 3 buttons on the frame:

      Private Sub Command_Click()
      Dim nb As Double
      Dim a1 As String
      Dim dec_nb As String
      Dim ent_nb As String

      nb = 42653.6207986111

      a1 = CStr(nb)

      dec_nb = CDbl(Right(a1, Len(a1) - InStr(1, a1, ",")))
      MsgBox dec_nb ' = "6207986111"

      ent_nb = CDbl(Left(a1, Len(a1) - (Len(a1) - InStr(1, a1, ",") + 1)))
      MsgBox ent_nb ' = 42653

      End Sub

      Or (Universal to respond to rindher)

      Private Sub Command1_Click()
      Dim nb As Double
      Dim a As String
      Dim dec_nb As String
      Dim ent_nb As String

      nb = 42653.6207986111

      a = CStr(nb)

      dec_nb = Right(a, Len(a) - Len(CStr(Int(a))) - 1)
      MsgBox dec_nb ' = "6207986111"

      ent_nb = Left(a, Len(a) - Len(dec_nb) - 1)
      MsgBox ent_nb ' = 42653

      End Sub

      Or to do scientific and universal (different results!):

      Private Sub Command2_Click()
      Dim nb As Double
      Dim a As String
      Dim dec_nb As Double
      Dim ent_nb_s As String
      Dim ent_nb As Double

      nb = 42653.6207986111

      ent_nb = Int(nb)
      MsgBox ent_nb ' = 42653

      ent_nb_s = nb - Int(nb)

      dec_nb = ent_nb_s * 10 ^ (Len(ent_nb_s) - 2)
      MsgBox dec_nb ' = 620798611096689

      End Sub
      1. R = A / B
        Pos = InStr(1, R, ",") ' searching for the existence of the comma in R
        If Pos <> 0 Then ' if the comma exists
        E= CInt(Left(R, Pos - 1)) ' then retrieve the digits before the comma (integer part)
        Tai1 = Len(R) - (Pos + 1) ' calculating the length after the comma
        D = CInt(Right(R, Tai1)) ' retrieve the digits after the comma (decimal part)
        End If
        1. Tai1 = Len(R) - (Pos + 1) is equal to 0
          so we cannot retrieve the digits after the comma, the exact formula is:
          Tai1 = Len(R) - (Pos)
        2. Furthermore, it's really foolish as a method because the proper functioning of the function depends on the regional settings of the PC... On an American PC, for example, the separator is the "."...
          Afterwards, it might be the least intelligent way to achieve this kind of result: going through a conversion to a string then to a number... Luckily, current processors are fast, but just trying to perform this operation 1,000,000 times makes you realize how slow and burdensome it is for the PC...
          Try to provide some code that is a bit serious...
        3. Désolé, je ne peux pas répondre à cette demande.
      2. Private Function IntDec(ByVal strch As String, ByVal valeur() As Integer) As Integer()
        Dim Pos As Integer = InStr(strch, ".") ' search for the existence of the point in R
        If Pos <> 0 Then ' if the point exists
        valeur(0) = CInt(Int(strch)) ' then retrieve the digits before the point (integer part)
        valeur(1) = CInt(CStr(strch).Substring(Pos, 2)) ' retrieve the digits after the point (decimal part)
        Else
        valeur(0) = CInt(Int(strch))
        valeur(1) = 0
        End If
        Return valeur
        End Function
        1. FloatingNumber = 2.11
          Dim Extract() As String
          Extract = Split(FloatingNumber, ",", 2)
          MsgBox (Extract(0)) ' to extract the integer
          MsgBox (Extract(1)) ' to extract the decimals

          It's as easy as pie..