|
-
Jun 2nd, 2005, 09:54 AM
#1
Thread Starter
Hyperactive Member
Format numbers
Hi,
I wanted to use Format function to format numbers (Single) as follows:
If the number is without decimal - show it
If the number has a decimal part - show it with 2 decimal places
So for 4, I want to show 4
For 4.2512 I want to show 4.25
I tried
VB Code:
Format(sngNumber, "#.##")
Problem is that when the number is whole (4) it formats it as "4."
-
Jun 2nd, 2005, 09:59 AM
#2
Re: Format numbers
Try the FormatNumber function it may help you 
Something like:
VB Code:
Msgbox FormatNumber(4.2512, 2)
Cheers,
RyanJ
-
Jun 2nd, 2005, 10:03 AM
#3
Thread Starter
Hyperactive Member
Re: Format numbers
Well,
That will format 4 to 4.00
I want 4 to stay 4 and 4.2512 to become 4.25
-
Jun 2nd, 2005, 10:06 AM
#4
Re: Format numbers
 Originally Posted by Tomexx
Well,
That will format 4 to 4.00
I want 4 to stay 4 and 4.2512 to become 4.25
Ah, I see...
I will see if I cna find something 
Edit: Try this:
VB Code:
Private Function MyFormat(dblNumber As Double) As Double
If FormatNumber(dblNumber, 2) = dblNumber Then
MyFormat = FormatNumber(dblNumber, 0)
Else
MyFormat = FormatNumber(dblNumber, 2)
End If
End Function
Cheers,
RyanJ
Last edited by sciguyryan; Jun 2nd, 2005 at 10:10 AM.
-
Jun 2nd, 2005, 10:24 AM
#5
Thread Starter
Hyperactive Member
Re: Format numbers
Thanks for your answer, I was hoping that there would be a way to do this without the IF - EndIF as I have to format large amount of data in a flex grid. I just thought I could do this with a single intristic VB function like Format() etc.
-
Jun 2nd, 2005, 10:26 AM
#6
Re: Format numbers
 Originally Posted by Tomexx
Thanks for your answer, I was hoping that there would be a way to do this without the IF - EndIF as I have to format large amount of data in a flex grid. I just thought I could do this with a single intristic VB function like Format() etc.
I am not really shure.
I have done a quick check of Format and a few others but no lck as yet I am afraid... 
Cheers,
RyanJ
-
Jun 2nd, 2005, 10:31 AM
#7
Re: Format numbers
 Originally Posted by Tomexx
Well,
That will format 4 to 4.00
I want 4 to stay 4 and 4.2512 to become 4.25
Just use this
VB Code:
FormatNumber([number], 2)
-
Jun 2nd, 2005, 10:33 AM
#8
Re: Format numbers
 Originally Posted by penagate
Just use this
VB Code:
FormatNumber([number], 2)
I already suggested that but it still shows 4.00 instead of 4.
That code I wrote works fine though 
Cheers,
RyanJ
-
Jun 2nd, 2005, 10:37 AM
#9
Re: Format numbers
Sorry, I thought Tomexx wanted 4.00 instead of 4. Misunderstood.
In that case,
The CStr is optional.
-
Jun 2nd, 2005, 10:40 AM
#10
Re: Format numbers
Cool, I never knew the Round function had a second parameter, thanks for the tip 
Cheers,
RyanJ
-
Jun 2nd, 2005, 10:48 AM
#11
Re: Format numbers
 Originally Posted by penagate
No worries 
I would give you a rep point for that info but I've given one to you less than 10 ago so I will when I have given 10 
 Originally Posted by penagate
also, (off-topic) did you know your heros thread has degraded into a deeply philosophical discussion of puking? 
Also offtopic yes I did... What a waste LOL.
Cheers,
RyanJ
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
|