Results 1 to 25 of 25

Thread: Remove decimal place (not saving variable as int)

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2013
    Location
    Connecticut
    Posts
    28

    Remove decimal place (not saving variable as int)

    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?

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2013
    Location
    Connecticut
    Posts
    28

    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

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Remove decimal place (not saving variable as int)

    Quote Originally Posted by diazdar View Post
    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?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jun 2013
    Location
    Connecticut
    Posts
    28

    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)
    Attached Images Attached Images  

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Remove decimal place (not saving variable as int)

    Take a look at the PadLeft method of string. What you want to do is pad to the size you want with "0"c
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jun 2013
    Location
    Connecticut
    Posts
    28

    Re: Remove decimal place (not saving variable as int)

    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?

  9. #9
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    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:
    1. Function GetNumber(ByVal theString As String) As String
    2.         Dim sa() As Char = theString.ToCharArray
    3.         Dim builder As New StringBuilder
    4.  
    5.         For Each n In sa
    6.             If IsNumeric(n) Then builder.Append(n)
    7.         Next
    8.         Return builder.ToString
    9.     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:
    1. Imports System.Text
    2.  
    3. Public Class Form1
    4.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    5.         Dim x As String = GetNumber("9.99")
    6.         Dim newbuilder As New StringBuilder
    7.  
    8.         If x.Count < 10 Then
    9.             MsgBox(x.Count)
    10.             newbuilder.Append("0", 10 - x.Count)
    11.             newbuilder.Append(x)
    12.         End If
    13.         x = newbuilder.ToString
    14.         MsgBox(x)
    15.  
    16.     End Sub
    17.     Function GetNumber(ByVal theString As String) As String
    18.         Dim sa() As Char = theString.ToCharArray
    19.         Dim builder As New StringBuilder
    20.  
    21.         For Each n In sa
    22.             If IsNumeric(n) Then builder.Append(n)
    23.         Next
    24.         Return builder.ToString
    25.     End Function
    26. End Class

    The Input in here is '9.99' and returns '0000000999'
    Last edited by Crzyrio; Jun 5th, 2013 at 02:49 PM.

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Remove decimal place (not saving variable as int)

    Quote Originally Posted by diazdar View Post
    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.
    My usual boring signature: Nothing

  11. #11
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    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:
    1. Dim x As String = "999"
    2. Dim newbuilder As New StringBuilder
    3.  
    4.         If x.Count < 10 Then
    5.             newbuilder.Append("0", 10 - x.Count)
    6.             newbuilder.Append(x)
    7.         End If
    8.  
    9. 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.

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Jun 2013
    Location
    Connecticut
    Posts
    28

    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?

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Jun 2013
    Location
    Connecticut
    Posts
    28

    Re: Remove decimal place (not saving variable as int)

    oh didnt use it right, give me a minute

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Jun 2013
    Location
    Connecticut
    Posts
    28

    Re: Remove decimal place (not saving variable as int)

    Yeah it doesnt' work. Any suggestions?

  15. #15
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Remove decimal place (not saving variable as int)

    Quote Originally Posted by diazdar View Post
    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...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Jun 2013
    Location
    Connecticut
    Posts
    28

    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

    will that work?

  17. #17
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    Re: Remove decimal place (not saving variable as int)

    Quote Originally Posted by diazdar View Post
    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:
    1. Imports System.Text
    2.      
    3.     Public Class Form1
    4.         Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    5.             Dim x As String = GetNumber("9.99")
    6.             Dim newbuilder As New StringBuilder
    7.      
    8.             If x.Count < 10 Then
    9.                 MsgBox(x.Count)
    10.                 newbuilder.Append("0", 10 - x.Count)
    11.                 newbuilder.Append(x)
    12.             End If
    13.             x = newbuilder.ToString
    14.             MsgBox(x)
    15.      
    16.         End Sub
    17.         Function GetNumber(ByVal theString As String) As String
    18.             Dim sa() As Char = theString.ToCharArray
    19.             Dim builder As New StringBuilder
    20.      
    21.             For Each n In sa
    22.                 If IsNumeric(n) Then builder.Append(n)
    23.             Next
    24.             Return builder.ToString
    25.         End Function
    26.     End Class

    Quote Originally Posted by diazdar View Post
    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:
    1. Dim newbuilder As New System.Text.StringBuilder
    2.  
    3.         If minimumValue.Count < 10 Then
    4.             newbuilder.Append("0", 10 - minimumValue.Count)
    5.             newbuilder.Append(minimumValue)
    6.         End If
    7. minimumValue = newbuilder.tostring
    Last edited by Crzyrio; Jun 5th, 2013 at 03:05 PM.

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Jun 2013
    Location
    Connecticut
    Posts
    28

    Re: Remove decimal place (not saving variable as int)

    lol i meant decimal sorry, not double

  19. #19
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Remove decimal place (not saving variable as int)

    well something isn't right... I used this code:
    Code:
            Dim minimumNumber As String = "99.99"
            minimumNumber = minimumNumber.Replace(".", "").PadLeft(10, "0"c)
            MessageBox.Show(minimumNumber)
    I get "000009999" in my messagebox....

    Name:  vbf-thenines.PNG
Views: 180
Size:  11.8 KB

    so there's no reason the PadLeft shouldn't be working...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  20. #20

    Thread Starter
    Junior Member
    Join Date
    Jun 2013
    Location
    Connecticut
    Posts
    28

    Re: Remove decimal place (not saving variable as int)

    that worked perfectly, thank you so much. Only one problem, if a user inputs 10 digits, the whole set of numbers disappears.

    Any suggestions?

  21. #21
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    Re: Remove decimal place (not saving variable as int)

    [

    vb.net Code:
    1. Dim newbuilder As New System.Text.StringBuilder
    2.      
    3.             If minimumValue.Count < 10 Then
    4.                 newbuilder.Append("0", 10 - minimumValue.Count)
    5.                 newbuilder.Append(minimumValue)
    6.                 minimumValue = newbuilder.tostring
    7.             End If
    Last edited by Crzyrio; Jun 5th, 2013 at 03:20 PM.

  22. #22

    Thread Starter
    Junior Member
    Join Date
    Jun 2013
    Location
    Connecticut
    Posts
    28

    Re: Remove decimal place (not saving variable as int)

    techgnome that was perfect. Solved all problems, thank you.

  23. #23

    Thread Starter
    Junior Member
    Join Date
    Jun 2013
    Location
    Connecticut
    Posts
    28

    Re: Remove decimal place (not saving variable as int)

    and Crzyrio thank you for your help, im also writing down that method in case. very useful stuff.

  24. #24

    Thread Starter
    Junior Member
    Join Date
    Jun 2013
    Location
    Connecticut
    Posts
    28

    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.

  25. #25
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    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:
    1. Const digits As String = "0123456789"
    2. Dim input As String = "$3, 457.99"
    3. Dim result As String = String.Concat(input.Where(Function(c) Char.IsDigit(c))).PadLeft(10, "0"c)
    4. 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.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width