Edit parts of string

BrandonMPSMI

Active member
Joined
Oct 8, 2008
Messages
43
Programming Experience
Beginner
I need help on my calculator again.

Basically after using the calculator you'll end up with "534.00" I want to cut the ".00" off of the string.

So what I need is

VB.NET:
If txt.display.text has a decimal with zeroes after it Then
  Delete those zeroes and the decimal
End If

I guess I'm starting to get into string Parsing or whatever you call it.
This is really simple (I think) So please help me!
 
VB.NET:
If txtDisplay.Text.Trim.ToLower.EndsWith(".00") Then txtDisplay.Text = txtDisplay.Text.Trim.SubString(0I, txtDisplay.Text.Trim.Length - 4I)
Might be Length - 3I instead of - 4I too
 
But what if the string is ".000"?

I need the code to just check if what's beyond the decimal equals nothing (I think) Otherwise I would have to do that for each and every amount of zeroes possible. right?
 
I'm assuming you want values like 534.100 to be shortened to 534.1

VB.NET:
	Private Function TrimZeros(ByVal numberAsStringToTrim As String) As String

		Return numberAsStringToTrim.TrimEnd("0").TrimEnd(".")

	End Function
 
Not sure how it's not working for you.

Stubbed in a MessageBox to show the original values and the values after trimming the zeros off the end and they all came out as expected.

VB.NET:
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	Handles MyBase.Load

		Dim input As String() = {"534.00", "534.001", "534.010", "534.100"}

		For i As Integer = 0 To input.Length - 1
			MessageBox.Show(String.Format("Original: {0}{1}Trimmed: {2}", input(i), Environment.NewLine, TrimZeros(input(i))))
		Next

	End Sub

	Private Function TrimZeros(ByVal numberAsStringToTrim As String) _
	As String

		Return numberAsStringToTrim.TrimEnd("0").TrimEnd(".")

	End Function
 
I get an error when pasteing this code.

Error 1 'Private Function TrimZeros(display As String) As String' and 'Private Function TrimZeros(numberAsStringToTrim As String) As Object' cannot overload each other because they differ only by return types. C:\Users\remote\Documents\Automation Calculator\Automation Calculator.vb 1087 22 Automation Calculator
 
Sounds like you already have a method called TrimZeros. Rename the one I sent you to TrimForDisplay or something else meaningful.
 
Back
Top