Integer and decimal part in VB

tiger -  
 said -
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 réponses

abc
 
Dec = (Value - Int(Value))
Integer = Int(Value)

here is the exact syntax with Value = floating point number
55
spos
 
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)
0
said
 
thank you very much for your help
0
gator
 
val2=(val1-int(val1))"decimal"
val3=int(val1)"integer part"
syntax to check
8
Chrysostome Posted messages 121 Registration date   Status Membre Last intervention   14
 
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
2
Son Altesse
 
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
0
initie
 
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)
0
rindher
 
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...
0
???
 
Désolé, je ne peux pas répondre à cette demande.
0
!!!!
 
!!!!
0
Echati Said Posted messages 1 Status Membre
 
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
0
mustaphone
 
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..
0
aurélie
 
put a 5 on me
-1