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.
Re: Remove decimal place (not saving variable as int)
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 "".
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
Re: Remove decimal place (not saving variable as int)
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
Re: Remove decimal place (not saving variable as int)
Dim input = TextBox1.Text.Replace("."c, "")
... at its simplest.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
Re: Remove decimal place (not saving variable as int)
Originally Posted by diazdar
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
What if they enter 99 or 99.9? This sounds like you are making this complex for no reason. Where are you saving the data?
Re: Remove decimal place (not saving variable as int)
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)
Re: Remove decimal place (not saving variable as int)
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'
Last edited by Crzyrio; Jun 5th, 2013 at 02:49 PM.
Re: Remove decimal place (not saving variable as int)
Originally Posted by diazdar
Code:
'padleft for values
Dim pad As Char
pad = "0"c
Console.WriteLine(minimumValue.PadLeft(10, pad))
I inserted that, but it doesnt pad values to the left. Is that wrong?
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.
Re: Remove decimal place (not saving variable as int)
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.
Re: Remove decimal place (not saving variable as int)
Code:
Dim newbuilder As New System.Text.StringBuilder
If minimumValue.Count < 10 Then
newbuilder.Append("0", 10 - minimumValue.Count)
newbuilder.Append(minimumValue)
End If
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?
Re: Remove decimal place (not saving variable as int)
Originally Posted by diazdar
Code:
Dim newbuilder As New System.Text.StringBuilder
If minimumValue.Count < 10 Then
newbuilder.Append("0", 10 - minimumValue.Count)
newbuilder.Append(minimumValue)
End If
minimum value is the variable.
But WHAT is the variable Dimmed as? That's what we're guessing at...
Re: Remove decimal place (not saving variable as int)
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
Re: Remove decimal place (not saving variable as int)
Originally Posted by diazdar
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
Originally Posted by diazdar
Code:
Dim newbuilder As New System.Text.StringBuilder
If minimumValue.Count < 10 Then
newbuilder.Append("0", 10 - minimumValue.Count)
newbuilder.Append(minimumValue)
End If
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?
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
Last edited by Crzyrio; Jun 5th, 2013 at 03:05 PM.
Re: Remove decimal place (not saving variable as int)
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.
Re: Remove decimal place (not saving variable as int)
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.
Last edited by AceInfinity; Jun 5th, 2013 at 07:46 PM.