Results 1 to 8 of 8

Thread: [RESOLVED] Removing decimal numbers from output

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Resolved [RESOLVED] Removing decimal numbers from output

    Hi all,

    Quick question: When you've calculated a number, if that number has a long decimal, how can you remove it before output?

    For example, if the variable "X" is storing the number 480.3392, how can you make it output just 480.33 ?

    The problem with this is that A) the number of X is unknown and B) the length of X is unknown, in other words it could be "8" or "4029.48560", so I can't perform a snip using Mid$. I just want to output the whole number and two decimal point numbers.

    Any ideas?

    Thanks

  2. #2
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: Removing decimal numbers from output

    use the Format function.

    VB Code:
    1. format(YourNumber, "0.00")

  3. #3
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Removing decimal numbers from output

    Depends what you want. You can format it.

    VB Code:
    1. msgbox format("234.238","#.##") ' 234.24

    or you can use InStr() to find the location of the decimal point, and add the next two characters.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Re: Removing decimal numbers from output

    Ahh! Awesome, thanks

    dglienna, how would I use the InStr command? (The syntax)

    Oh, I'm also trying to put some code into a text cbox to stop people from putting two .s in and causing it to crash.. my current code works for two .s in a row, but is there an easier way of doing it when they are placed apart?

    Current code:

    Code:
    Private Sub ICost_KeyPress(KeyAscii As Integer)
    MsgBox WatchDots
    If WatchDots = True Then
        If (KeyAscii = 46) Then
            MsgBox "Error! You may only have one .", vbCritical, "Error!"
            KeyAscii = 0
            ICost.Text = ""
            WatchDots = False
            Exit Sub
        End If
    End If
    
    If (KeyAscii = 46) Then
        WatchDots = True
    ElseIf (KeyAscii = 8) Or (KeyAscii < 48) Or (KeyAscii > 57) Then
        WatchDots = False
    End If
    
    If (KeyAscii = 8) Or (KeyAscii = 9) Or (KeyAscii = 46) Then Exit Sub
    If (KeyAscii < 48) Or (KeyAscii > 57) Then
        KeyAscii = 0
        MsgBox "Please input only a number in the form: 45 or: 21.90 (Examples)"
    End If
    End Sub
    It sort-of works (since the max length is 6, so it makes it a bit tricky to bypass easily, but it still possible). I could remove the line that makes it false, but then it would stay as "true" after they clear the textbox and type a new value, and would stop them typing a new value. There's a couple of possible work-arounds that I thought of, including having it without the line that makes it false, but them when they hit "Calculate", making it clear the text fields and reset it to false, or having a "Clear boxes" button that clears the values and sets it to false.. but I'm sure there's an easier way than that?

  5. #5
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Removing decimal numbers from output

    This finds the first "." in a string.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.   Dim nm As String
    5.   Dim x As Integer
    6.   nm = "234.239999"
    7.   x = InStr(nm, ".")
    8.   MsgBox Left$(nm, x + 2) ' 234.23
    9. End Sub

    and add's 2, to include the two characters afterwards.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Re: Removing decimal numbers from output

    Ahh, that's great too, thanks!

    Any ideas for the second part of my problem in the last question? (Tacking .s to make sure there aren't two of them in the same text field)


    Ohh, also, I've just implemented some code that I found here to make the program always on top:

    Code:
    Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
    Private Const HWND_TOPMOST = -1
    Private Const HWND_NOTOPMOST = -2
    Private Const SWP_NOMOVE = 2
    Private Const SWP_NOSIZE = 1
    Private Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE
    Code:
    SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS
    What would the opposite of that call (SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS) be? In other words, to make it not always on top anymore. I currently have it in a yes/no messagebox on form_load but I'd like to make it an "Always on top checkbox"

    Thanks ^_^
    Last edited by BubbleLife; Jan 7th, 2006 at 02:50 AM.

  7. #7
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Removing decimal numbers from output

    You may want to limit the input to only numbers. There is code on the forums that let you do this. Whatever you do, you'd have to reset the counter at some point, if the user is going to enter a new number. You could use the masked edit control to format the text as they type, if it always has to be a certain format, but that would require that they enter every character before they left the control. It's good for phone numbers and zip codes.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Re: Removing decimal numbers from output

    I was going to limit to only numbers, but the problem is they need to be able to enter a . for a decimal point.. I guess I could make it whole numbers only, a few pence either way should matter that much anyway.

    Thanks ^_^

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