-
Mar 20th, 2019, 09:06 AM
#1
Thread Starter
New Member
(deleted]
Last edited by VBHELP2000; Mar 27th, 2019 at 06:29 PM.
-
Mar 20th, 2019, 09:20 AM
#2
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.
-
Mar 20th, 2019, 09:23 AM
#3
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.
-
Mar 20th, 2019, 10:06 AM
#4
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 20th, 2019, 10:30 AM
#5
Thread Starter
New Member
Re: VB formatting all subs for decimal places
Last edited by VBHELP2000; Mar 27th, 2019 at 06:28 PM.
-
Mar 20th, 2019, 10:30 AM
#6
Thread Starter
New Member
Re: VB formatting all subs for decimal places
Last edited by VBHELP2000; Mar 27th, 2019 at 06:28 PM.
-
Mar 20th, 2019, 11:42 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|