So I have an input box and a user would enter an amount. Lets say a user enters 999.99, I want the variable to save as 99999. Basically I want to save exactly what the user enters without the decimal point.
How do I do this? :confused:
Printable View
So I have an input box and a user would enter an amount. Lets say a user enters 999.99, I want the variable to save as 99999. Basically I want to save exactly what the user enters without the decimal point.
How do I do this? :confused:
Well, first of all, why would you? If a user enters 999.99, I can't see how you would decide that it really means 99999! But if you insist, use the string .Replace method to replace "." with String.Empty or "".
Haha I know, I'm making a program that takes input information the user types in and encodes it into text readable by a promotion coupon application. Appreciate all your help man.
so 9.99 is what the user inputs, the application understands that as 999. 2 dec to the left
Dim input = TextBox1.Text.Replace("."c, "")
... at its simplest.
Okay that was a great question that basically put me into an epic fail situation. Check out the attached picture. Third row down look over to the minimum and maximum digits. the minimum has a bunch of zeros and the maximum has all 9s. The last two digits to the right of both boxes indicated values past the decimal place and the 8 digits before that represent whole numbers. How can I code this application so that if someone enters 9.99
it will come up as
0000000999
my application doesnt product zeros based on the amount the user inputs. if i enter 9.99, you'll literally see 999
the only reason the minimum shows all those zeros before the 9999 is because i added them myself ("000000" & minAmount)
Take a look at the PadLeft method of string. What you want to do is pad to the size you want with "0"c
I inserted that, but it doesnt pad values to the left. Is that wrong?Code:'padleft for values
Dim pad As Char
pad = "0"c
Console.WriteLine(minimumValue.PadLeft(10, pad))
Someone on here, gave this to me a little while back. I am sure you will find some use of it :)
vb.net Code:
Function GetNumber(ByVal theString As String) As String Dim sa() As Char = theString.ToCharArray Dim builder As New StringBuilder For Each n In sa If IsNumeric(n) Then builder.Append(n) Next Return builder.ToString End Function
Function breaks up the string in to a array of characters and then checks each if it is numerical. If it is then it adds it to the string and lastly returns that string back to you.
Sample Program :)
vb.net Code:
Imports System.Text Public Class Form1 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Dim x As String = GetNumber("9.99") Dim newbuilder As New StringBuilder If x.Count < 10 Then MsgBox(x.Count) newbuilder.Append("0", 10 - x.Count) newbuilder.Append(x) End If x = newbuilder.ToString MsgBox(x) End Sub Function GetNumber(ByVal theString As String) As String Dim sa() As Char = theString.ToCharArray Dim builder As New StringBuilder For Each n In sa If IsNumeric(n) Then builder.Append(n) Next Return builder.ToString End Function End Class
The Input in here is '9.99' and returns '0000000999'
Well, if it doesn't pad values, then it is certainly wrong, but it doesn't look all that bad. What is minimumValue? That sounds like it might not be a string, and you are padding with 0s, so if it is a numeric type, and especially if you have Option Strict OFF, I would guess that it is implicitly converting your value to a string, padding, then implicitly converting back to the numeric type, which will delete off all the 0s again.
For the 0's you can also use the string builder, it is actually quiet usefull.
vb.net Code:
Dim x As String = "999" Dim newbuilder As New StringBuilder If x.Count < 10 Then newbuilder.Append("0", 10 - x.Count) newbuilder.Append(x) End If x = newbuilder.ToString
Since the maximum number of characters you want is 10, check if your string has less than 10 characters. If it does then you add 0's to it based on 10 subtract the number of values in your string and then append your string again and you are set.
If you put this piece of code in my above example you can enter 9.99 and it will output 0000000999.
minimum value is the variable. Is that correct? It still doesnt work. I changed line 1 because there was a syntax error. Is that even right?Code:Dim newbuilder As New System.Text.StringBuilder
If minimumValue.Count < 10 Then
newbuilder.Append("0", 10 - minimumValue.Count)
newbuilder.Append(minimumValue)
End If
oh didnt use it right, give me a minute
Yeah it doesnt' work. Any suggestions?
Dim minimumValue As String
if i change it, it renders ' minimumValue = minimumValue.Replace(".", "")'
as useless since that is for a string. How can I use a replace function to change the decimal to nothing,
and if i change it to a double
will that work?
Take a look at this and see if it makes sense
vb.net Code:
Imports System.Text Public Class Form1 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Dim x As String = GetNumber("9.99") Dim newbuilder As New StringBuilder If x.Count < 10 Then MsgBox(x.Count) newbuilder.Append("0", 10 - x.Count) newbuilder.Append(x) End If x = newbuilder.ToString MsgBox(x) End Sub Function GetNumber(ByVal theString As String) As String Dim sa() As Char = theString.ToCharArray Dim builder As New StringBuilder For Each n In sa If IsNumeric(n) Then builder.Append(n) Next Return builder.ToString End Function End Class
TRY THIS :
vb.net Code:
Dim newbuilder As New System.Text.StringBuilder If minimumValue.Count < 10 Then newbuilder.Append("0", 10 - minimumValue.Count) newbuilder.Append(minimumValue) End If minimumValue = newbuilder.tostring
lol i meant decimal sorry, not double
well something isn't right... I used this code:
I get "000009999" in my messagebox....Code:
Dim minimumNumber As String = "99.99"
minimumNumber = minimumNumber.Replace(".", "").PadLeft(10, "0"c)
MessageBox.Show(minimumNumber)
Attachment 100841
so there's no reason the PadLeft shouldn't be working...
-tg
that worked perfectly, thank you so much. Only one problem, if a user inputs 10 digits, the whole set of numbers disappears.
Any suggestions?
[
vb.net Code:
Dim newbuilder As New System.Text.StringBuilder If minimumValue.Count < 10 Then newbuilder.Append("0", 10 - minimumValue.Count) newbuilder.Append(minimumValue) minimumValue = newbuilder.tostring End If
techgnome that was perfect. Solved all problems, thank you.
and Crzyrio thank you for your help, im also writing down that method in case. very useful stuff.
im going to publish this application online for free since theres countless businesses using this corporate retail system and theres no applications to make uploading coupons easy like the one im making. im going to put everyone who helped me as a developer for this program, message me your name or alias if you helped me and would like to be included.
In my opinion you're using the wrong control anyways. You should use a NumericUpDown control for this task.
edit: I may have misread the issue here. What if they enter $99.00 though. Removing the "." does not remove the "$". This is why I usually try to program for the value that I know I want. You know you want digits from 0-9. So in my opinion instead of removing values, you should be looking for the characters to keep. They could also enter in data like this: "$100, 000.00" where there may be a comma and a space.
This can be achieved with (a little) less code using LINQ:
vbnet Code:
Const digits As String = "0123456789" Dim input As String = "$3, 457.99" Dim result As String = String.Concat(input.Where(Function(c) Char.IsDigit(c))).PadLeft(10, "0"c) Console.WriteLine(result) '0000345799
Or you can loop with the regular For loop and check the values.