Re: Decimal Formatting Help
Well, w/o seeing the code you have, it's hard to say what you should be doing differently.
-tg
Re: Decimal Formatting Help
When you call ToString on a numeric value, you can provide a format specifier to control what the output looks like. If you want one decimal place in the output then you can use "n1" or "f1" as the specifier. Change the number in the specifier to change the number of decimal places. You should read about numeric format strings in the MSDN documentation to learn more about what options there are.
Re: Decimal Formatting Help
Added code, sorry about that
Re: Decimal Formatting Help
I read up on that before I came onto this post I made but where I thought I would put it which is where I declare what rtbDisplay.Text equals, just crashes the program. That is the main issue of finding where to put it lol. Putting it in the while statement also crashes program too.
Re: Decimal Formatting Help
You can only use a numeric format string when converting an actual number to a String. If you already have a String then you can't format it as a number. The place that you convert a number to a String is here:
Code:
Data += startNum & vbNewLine
so that's where you should do the formatting:
Code:
Data &= startNum.ToString("n2") & vbNewLine
Notice that I also changed '+=' to '&=". While you can use '+' to concatenate Strings in VB.NET, you should never do so. You should ALWAYS use '&'. That's because '+' is actually the addition operator and, while addition of two Strings is implemented as concatenation, addition of one String and something else is generally not, so '+' may not behave as expected in some circumstances. '&' is the concatenation operator and will ALWAYS behave as expected.
Re: Decimal Formatting Help
Here are my declarations:
Code:
Dim startNum, starting, custom, progress As Decimal
Dim total As Long
Dim EndNum, increment As Decimal
Dim Data, numdata As String
Dim counter As Integer = 0
Dim time As Integer = 1
Dim TimeCount As Integer = 0
Dim valprint As String
I should make Data and Startnum and Endnum into string variables instead of Decimal variables? Therefore getting the results I want?
Re: Decimal Formatting Help
I tried that(hopefully the correct way) and it didn't work. I tried changing it to different ways but those didn't work either. I changed the startnum declaration to a string instead of a decimal like I thought would be the way to do it.
Re: Decimal Formatting Help
Post the code that you think is right and tell us what actually happens when you run it. "It doesn't work" is rarely very helpful. We need to know what doesn't work and how it doesn't work in order to know what needs to be done to fix it.
Re: Decimal Formatting Help
Code:
Dim startNum, starting, custom, progress As String 'Changed to String
Dim total As Long
Dim EndNum, increment As Decimal
Dim Data, numdata As String
Dim counter As Integer = 0
Dim time As Integer = 1
Dim TimeCount As Integer = 0
Dim valprint As String
I changed the variable from a decimal to a string.
Code:
Data &= (startNum.ToString("n2") & vbNewLine) 'Can't Go Here
I then convert the string to a string? That doesn't look right but I am still learning.
After I run the program, the program crashes with an exception unhandled error.
Re: Decimal Formatting Help
What did I say in post #6?
Quote:
You can only use a numeric format string when converting an actual number to a String. If you already have a String then you can't format it as a number.
Now let's look at your code:
Quote:
Originally Posted by
Dragnorian
Code:
Dim startNum, starting, custom, progress As String 'Changed to String
Dim total As Long
Dim EndNum, increment As Decimal
Dim Data, numdata As String
Dim counter As Integer = 0
Dim time As Integer = 1
Dim TimeCount As Integer = 0
Dim valprint As String
I changed the variable from a decimal to a string.
Code:
Data &= (startNum.ToString("n2") & vbNewLine) 'Can't Go Here
I then convert the string to a string? That doesn't look right but I am still learning.
After I run the program, the program crashes with an exception unhandled error.
Do you not see the issue there? People are commonly quite lax with data types when they start out and this is a perfect example of why they need to tighten up ASAP. If, as the name suggests, 'startNum' is supposed to represent a number then it should be declared as the appropriate numeric type. String is ONLY for text. If you want to treat something as a number then it needs to be a number, NOT text. You only convert numbers, dates, etc to text for the purposes of display or serialisation, e.g. saving to a text file. In your code, numbers should ALWAYS be stored in numeric variables.
Re: Decimal Formatting Help
Yes I know that I Need to tighten up on it. I figured that was a bit strange, I would be lying if I said I never misread anything. I got it all now, thank you for teaching me this and sorry for the accident I made. This will surely help my program. I had been trying different formats(attempted) before coming here. Thank you!
Re: Decimal Formatting Help
Quote:
Originally Posted by
Dragnorian
Yes I know that I Need to tighten up on it.
Pretty much everyone does to begin with. I think that it's often because data goes into and out of a console application as text and VB in particular can be quite forgiving in many cases, performing implicit conversions much of the time. Proper use of data types is critical to create software of any significance though.
Re: Decimal Formatting Help
Yeah, I will be working on that. Thank you again for the help!
Re: Decimal Formatting Help
Re: Decimal Formatting Help
That whole formatting issue was because the user can predetermine a set increment value that I have defined in the code but what about a custom increment value that the user defines, not me? I thought doing something like .ToString("n#") would work but no. I need something to figure out how it can calculate if the custom value is either an integer or a decimal number then figure out if decimal, how many places it should go to then do the same basic format as the .ToString("n1") does. Is this possible in Visual Basic?.
Re: Decimal Formatting Help
E.g.
vbn.et Code:
Private Function GetFormatSpecifier(increment As Double) As String
Dim formatSpecifier As String
Dim text = increment.ToString()
If text.Contains(".") Then
Dim fraction = text.Substring(text.IndexOf(".") + 1)
formatSpecifier = "n" & fraction.Length
Else
formatSpecifier = "n0"
End If
Return formatSpecifier
End Function
That's untested but I believe it should do the job. You can test and adjust if required.
Re: Decimal Formatting Help
Re: Decimal Formatting Help
I've never worked with functions before, so where would I put it? Inside the button?
Re: Decimal Formatting Help
If your using a form then just add it to your form. If your going to be calling it from multiple forms then declare the function "Public" and add it to a Module.