Results 1 to 5 of 5

Thread: extracting whole number from large decimal value

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2007
    Posts
    31

    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!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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:
    1. Dim value As Decimal
    2.  
    3. If Decimal.TryParse(myTextBox.Text, value) Then
    4.     Dim int As Integer = CInt(Decimal.Truncate(value))
    5.  
    6.     'Use int here.
    7. Else
    8.     'TextBox does not contain a valid number.
    9. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    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.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Apr 2007
    Posts
    31

    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:
    1. Dim value As Decimal
    2.  
    3. If Decimal.TryParse(myTextBox.Text, value) Then
    4.     Dim int As Integer = CInt(Decimal.Truncate(value))
    5.  
    6.     'Use int here.
    7. Else
    8.     'TextBox does not contain a valid number.
    9. 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)

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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:
    1. 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.
    is there a simple math function to get the decimal part of a value? (just like truncate for the whole number)
    vb.net Code:
    1. Dim fraction As Decimal = value - Decimal.Truncate(value)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width