Results 1 to 7 of 7

Thread: (deleted]

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2019
    Posts
    13

    Post (deleted]

    (deleted]
    Last edited by VBHELP2000; Mar 27th, 2019 at 06:29 PM.

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

    Re: VB formatting subs for decimal places

    Again, please don't post images of code. Code is text, so post it as text, then format it appropriately as code. We can then copy and paste the code to run it for ourselves.

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

    Re: VB formatting subs for decimal places

    Firstly, Console.ReadLine returns a String and you are assigning it to a Double variable. What happens if the user enters "Hello World"?

    Apart from that, don't use the Format method at all. The Double value itself has a ToString method you should use to create a formatted String. Of course, you then need to do something useful with that String, e.g. assign it to a variable and/or write it to the console.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: VB formatting all subs for decimal places

    Try this...

    Code:
    Module Module1
    
        Sub Main()
            Dim places As Integer = 0
            Dim userdecimal As Decimal = 0
            Console.WriteLine("Please enter decimal to format")
            If Decimal.TryParse(Console.ReadLine, userdecimal) Then
                Console.WriteLine("Please enter the number of decimal places <1-5>")
                If Integer.TryParse(Console.ReadLine, places) AndAlso places >= 1 AndAlso places <= 5 Then
                    Console.WriteLine("Decimal places set to " & places.ToString)
                    Dim output As String = userdecimal.ToString("0." & StrDup(places, "0").ToString)
                    Dim parts() As String = output.Split("."c)
                    If CInt(parts(1)) = 0 Then
                        output = parts(0)
                    End If
                    Console.WriteLine(output)
                    Console.ReadLine()
                End If
            End If
        End Sub
    
    End Module

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2019
    Posts
    13

    Re: VB formatting all subs for decimal places

    (deleted]
    Last edited by VBHELP2000; Mar 27th, 2019 at 06:28 PM.

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2019
    Posts
    13

    Re: VB formatting all subs for decimal places

    (deleted]
    Last edited by VBHELP2000; Mar 27th, 2019 at 06:28 PM.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: VB formatting subs for decimal places

    Keep in mind that there is a difference between display and storage. Displaying a decimal to a set number of decimal places is pretty easy, as it's the .ToString("Nx"), where x is the number of decimal places you want displayed. However, that is just the display of the Decimal, while the Decimal itself will hold as many digits as it can.

    You might want to be truncating the Decimal using some form of rounding for each intermediate step, since there are cases where anything beyond a certain point is pseudo-precision, but that generally shouldn't be taken into account until the final answer is achieved. Let the data type hold as much precision as it can through each step of the calculation, and only round it at the end, if at all. The .ToString formatting will handle that well enough.
    My usual boring signature: Nothing

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