Results 1 to 21 of 21

Thread: [RESOLVED] changing a decimal into a whole number

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    13

    [RESOLVED] changing a decimal into a whole number

    Hey everyone.

    I am attempting to take the decimal places of a given input, round it so there are only two decimal places, then change it to a whole number. So far I have been able to remove the integer part of the number, leaving only the decimal, but the other procedures are eluding me. Any help would be appreciated. Here's what I have so far: (VB 2008)

    Code:
    Private Function DecimalNumber(ByVal number As Double) As Double
    
            Dim Input As Double
            Dim SubtractNumber As Decimal
            Dim Result As Double
    
            Input = Math.Truncate(number)
    
            SubtractNumber = number - Input
    
            Return Result
    
        End Function
    That code returns the decimal part of the input just fine, and I thought maybe multiplying it by 10 would work....but obviously you run into problems when there is more than one decimal place. Thanks!
    Last edited by dmaar; Apr 1st, 2011 at 08:32 AM.

  2. #2
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Re: changing a decimal into a whole number

    You could try using the Math.Round() function. It allows you to change the amount of decimal places a double value has.

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    13

    Re: changing a decimal into a whole number

    That worked great in reducing the number of places, thanks. Now all I need to do is convert it to a whole number. For example, if the value is ".24" it needs to just be "24". Here's the new code with the round function:

    Code:
        Private Function DecimalNumber(ByVal number As Double) As Double
    
            Dim Input As Double
            Dim SubtractNumber As Decimal
            Dim Result As Double
    
            Input = Math.Truncate(number)
    
            SubtractNumber = number - Input
    
            Result = Math.Round(SubtractNumber, 2)
    
            Return Result
    
        End Function

  4. #4
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: changing a decimal into a whole number

    You can not round a whole number, so you need to round the number first to 2 places first.

  5. #5
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: changing a decimal into a whole number

    After what you are doing you just need to multiply with 100.

    Try this:
    vb Code:
    1. Private Function DecimalNumber(ByVal number As Double) As Double
    2.     Return Fix(100 * (number - Fix(number) + 0.00000000001))
    3. End Function
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog • 101 LINQ Samples • JSON Validator • XML Schema Validator • "How Do I" videos on MSDN • VB.NET and C# Comparison • Good Coding Practices • VBForums Reputation Saver • String Enum • Super Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    13

    Re: changing a decimal into a whole number

    You're right Pradeep, that works for the particular example I posted and all decimals with 2 places, but when you get a decimal with only one place you run into problems with that equation. For example, if the decimal was ".2", .2 * 100 = 20, not 2.

    Is there a way to do something conditional? So if the number of places = 2, then it multiplies by 100, and if the places is 1 then it multiplies by 10. So it would be something like this:

    If (number of places after decimal point = 2) Then
    DecimalNumber * 100
    ElseIf (number of places after decimal point = 1) Then
    DecimalNumber * 10

    Thanks!

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: changing a decimal into a whole number

    HUH? What?
    Code:
            Dim foo As Double = Math.PI 'a test number with more than two decimals ;)
            Dim bar As Double = Math.Round(foo, 2) 'two decimals
    
            Debug.WriteLine(foo.ToString)
            Debug.WriteLine(bar.ToString)
            Stop
    
            Dim d As Double = bar * 100 'get rid of decimals
            Debug.WriteLine(d.ToString)
            Stop
    
            bar = Math.Round(foo, 1) 'one decimal
            Debug.WriteLine(bar.ToString)
            d = bar * 100 'get rid of decimals
            Debug.WriteLine(d.ToString)
            Stop
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,289

    Re: changing a decimal into a whole number

    Try this:
    Code:
    Private Function FixMyNumber(ByVal input As Double) As Integer
            Dim inputAsString As String = input.ToString("#.00")
            Dim decimalPart As String = inputAsString.Substring(inputAsString.IndexOf("."c) + 1)
            Return Integer.Parse(decimalPart)
        End Function
    Last edited by stanav; Mar 31st, 2011 at 10:26 AM. Reason: Changed the format string from "f2" to "#.00" to ensure it will work in any culture
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  9. #9
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: changing a decimal into a whole number

    Try this:
    vb.net Code:
    1. Private Function DecimalNumber(ByVal number As Double) As Double
    2.     number = 10 * (number - Fix(number) + 0.0000000000001)
    3.     If Math.Round(number, 9) <> Fix(number) Then number *= 10
    4.     Return Fix(number)
    5. End Function
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog • 101 LINQ Samples • JSON Validator • XML Schema Validator • "How Do I" videos on MSDN • VB.NET and C# Comparison • Good Coding Practices • VBForums Reputation Saver • String Enum • Super Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  10. #10
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: changing a decimal into a whole number

    If you want just the decimal part of a number

    Code:
            Dim foo As Double = Math.PI 'a test number with more than two decimals ;)
            Dim dp As Double = Math.Round(foo - Math.Floor(foo), 2)
    If the number can be negative then

    Code:
            Dim foo As Double = -Math.PI 'a test number with more than two decimals ;)
            Dim dp As Double
            If foo > 0 Then dp = Math.Round(foo - Math.Floor(foo), 2) Else dp = Math.Round(foo - Math.Ceiling(foo), 2)
    Last edited by dbasnett; Mar 31st, 2011 at 10:39 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  11. #11
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: changing a decimal into a whole number

    Moo:

    Code:
    Val(Microsoft.VisualBasic.Right((Math.Round(Foo,2)).ToString("#.0#"),2).Trim("."c))
    There are 5 cases

    1 = 0
    1.2 = 2
    1.23 = 23
    1.234 = 23
    0.04 = 4
    Last edited by Grimfort; Mar 31st, 2011 at 04:53 PM.

  12. #12
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: changing a decimal into a whole number

    Quote Originally Posted by dmaar View Post
    Hey everyone.

    I am attempting to take the decimal places of a given input, round it so there are only two decimal places, then change it to a whole number.... Thanks!
    Maybe we should know why you are taking the decimal part and trying to convert it to a whole number. The answer might be different if we knew more.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  13. #13

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    13

    Re: changing a decimal into a whole number

    Thanks everybody. Solved that problem...but it created another.

    What I ended up doing:
    After the decimal is separated from the whole number, the length of the value is either equal to four or three, depending on whether there is one decimal place or two.

    So I am just using an If statement to see whether it is 2 dec places or 1, and then using a substring(2,2) or (2,1) to remove the decimal place. Works pretty well.

    The problem happens when I get a value like ".04". In that case, even though the end value shows up in the code as "04" as I want it to, it always print just "4". How could I fix this?

  14. #14
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: changing a decimal into a whole number

    have you tried post #9. It works ok unless I have misunderstood something.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog • 101 LINQ Samples • JSON Validator • XML Schema Validator • "How Do I" videos on MSDN • VB.NET and C# Comparison • Good Coding Practices • VBForums Reputation Saver • String Enum • Super Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  15. #15
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: changing a decimal into a whole number

    I just edited mine, should work with 0.04 as well.

  16. #16

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    13

    Re: changing a decimal into a whole number

    Thanks guys! Finally got one working the way i want it.

  17. #17
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: changing a decimal into a whole number

    Show us then.

  18. #18
    Lively Member polecat's Avatar
    Join Date
    Jul 2005
    Location
    Wolverhampton
    Posts
    83

    Re: changing a decimal into a whole number

    This should work I think

    vb Code:
    1. Return Cint(result)

  19. #19
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: changing a decimal into a whole number

    I'm fairly sure that this should work for all scenarios:
    vb.net Code:
    1. Dim s = input.ToString("n2")
    2.  
    3. s = s.TrimEnd("0"c)
    4. s = s.Substring(s.IndexOf(".") + 1)
    5.  
    6. Dim output = CInt(s)
    If you want to allow for systems that use commas for decimal separators then you can get the decimal separator for the current culture.

  20. #20

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    13

    Re: changing a decimal into a whole number

    Quote Originally Posted by Grimfort View Post
    Show us then.
    Here's the basic idea (this is after rounding the decimal out to two digits):

    Code:
            Length = CStr(Len(DecimalString) - 2)
    
            If CDbl(Length) = 2 Then
                Result =  'Corresponding function Substring(2,2)
            ElseIf CDbl(Length) = 1 Then
                Result = 'Corresponding function Substring(2,1)
            End If

    If there are 2 decimal places after the round, the length will always be "4". If there is 1 decimal place after the round, the length will always be "3". I subtract 2 from the length (just to make it easier to follow), then use If statements to send the decimal to it's corresponding function, depending on whether there are 2 decimal places or 1. It is sent as a substring in order to get rid of the decimal places.

    Alternatively, you could use those length conditions to remove the decimal place, by multiplying it by 100 for the first and 10 for the second. However I found that the problem with this was that if you have something like ".04", you get "4" when multiplied by 100. I wanted to keep the "0" as well as the 4, and the substring method does that.

    I didn't do it exactly like anyone posted, but you're responses definitely helped me understand this better and figure one out for myself. This one may seem less logical, but I'm still pretty new at this.


    One last question: Does anyone know why when I subtract the truncated number from the input, the decimal it returns is often not right? For example:

    Code:
    DecimalNO = InputNO - TruncatedNO
    Where InputNO is 8.46, TruncatedNO is 8, ALWAYS gives me "0.46000000000000085". What's up with that? It should just be 0.46.

    The rounding takes care of this, but ideally I would like to throw an exception if the DecimalNO is greater than 2...this problem makes this hard to do.
    Last edited by dmaar; Apr 1st, 2011 at 09:46 AM. Reason: OCD

  21. #21
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: [RESOLVED] changing a decimal into a whole number

    Code:
            Dim iStr As String = "-8.46"
    
            Dim DecimalNO As Double
            Dim InputNO As Double
            Dim TruncatedNO As Double
    
            If Double.TryParse(iStr, InputNO) Then
                'good input value
                If InputNO > 0 Then
                    TruncatedNO = Math.Floor(InputNO)
                Else
                    TruncatedNO = Math.Ceiling(InputNO)
                End If
                DecimalNO = Math.Round(InputNO - TruncatedNO, 2)
            End If
            Debug.WriteLine("Input {0}, Truncated {1}, Decimal {2}", _
                            InputNO, TruncatedNO, DecimalNO)
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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