extracting whole number from large decimal value
Hi,
Objective: Extract the integer part of the number without rounding off and put it into a string
i.e. 987.999 = 987
method:
Code:
dim temp as string
temp = CDec(Fix(textbox1.Text))
problem: My code works when the value is less than 16 digits long. But when it is longer than 16 digits (including decimal pionts) it will round the value off, instead of just clipping off the decimal pionts.
Can anyone tell me why that is? Also, if there is another more effecient method to extract the whole number?
Thanks!
Re: extracting whole number from large decimal value
First up, take a look at that code and ask yourself what each stage is doing. First textbox1.Text returns a String, then Fix implicitly converts that to a number and truncates it to an Integer, then CDec converts it to a Decimal, then you implicitly convert it to a String again by assigning it to a String variable.
If you want to get the integral part of a decimal number entered into a TextBox then the "proper" way would be:
vb.net Code:
Dim value As Decimal
If Decimal.TryParse(myTextBox.Text, value) Then
Dim int As Integer = CInt(Decimal.Truncate(value))
'Use int here.
Else
'TextBox does not contain a valid number.
End If
Re: extracting whole number from large decimal value
Use the Math.Truncate method to get the integral part.
Code:
Dim t As Decimal = CDec(Math.Truncate(53.6)) ' = 53
Edit * aslo as shown above post.
Re: extracting whole number from large decimal value
Quote:
Originally Posted by jmcilhinney
First up, take a look at that code and ask yourself what each stage is doing. First textbox1.Text returns a String, then Fix implicitly converts that to a number and truncates it to an Integer, then CDec converts it to a Decimal, then you implicitly convert it to a String again by assigning it to a String variable.
If you want to get the integral part of a decimal number entered into a TextBox then the "proper" way would be:
vb.net Code:
Dim value As Decimal
If Decimal.TryParse(myTextBox.Text, value) Then
Dim int As Integer = CInt(Decimal.Truncate(value))
'Use int here.
Else
'TextBox does not contain a valid number.
End If
Thank you BOTH very much for your reply! I will use VBDT's method since it looks easier.
As for why my idea doesn't work, is it because, fix will convert the string to a integer type? And so, all the data beyond the integer range, will be loss? before i convert it to a decimal type?
But it seems like your method will lose data too, if we are using an int. Maybe i am not understand your code correctly.
Finally, is there a simple math function to get the decimal part of a value? (just like truncate for the whole number)
Re: extracting whole number from large decimal value
VBDT's code looks simpler than mine because it concentrates on just truncating the number. That part of my code is no more complex that VBDT's:
vb.net Code:
Dim int As Integer = CInt(Decimal.Truncate(value))
The rest of my code is to ensure that the user has entered a valid number. What are you going to do if the user enters "Hello World" into your TextBox? You can't truncate that because it's not a number. If you don't prevent that in the first place or test for it before or while converting then you app will crash.
Quote:
is there a simple math function to get the decimal part of a value? (just like truncate for the whole number)
vb.net Code:
Dim fraction As Decimal = value - Decimal.Truncate(value)