|
-
Jan 7th, 2006, 01:53 AM
#1
Thread Starter
Addicted Member
[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
-
Jan 7th, 2006, 02:08 AM
#2
Re: Removing decimal numbers from output
use the Format function.
VB Code:
format(YourNumber, "0.00")
-
Jan 7th, 2006, 02:08 AM
#3
Re: Removing decimal numbers from output
Depends what you want. You can format it.
VB Code:
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.
-
Jan 7th, 2006, 02:20 AM
#4
Thread Starter
Addicted Member
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?
-
Jan 7th, 2006, 02:36 AM
#5
Re: Removing decimal numbers from output
This finds the first "." in a string.
VB Code:
Option Explicit
Private Sub Form_Load()
Dim nm As String
Dim x As Integer
nm = "234.239999"
x = InStr(nm, ".")
MsgBox Left$(nm, x + 2) ' 234.23
End Sub
and add's 2, to include the two characters afterwards.
-
Jan 7th, 2006, 02:46 AM
#6
Thread Starter
Addicted Member
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.
-
Jan 7th, 2006, 03:04 AM
#7
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.
-
Jan 7th, 2006, 03:23 AM
#8
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|