Results 1 to 11 of 11

Thread: Format numbers

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    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:
    1. Format(sngNumber, "#.##")

    Problem is that when the number is whole (4) it formats it as "4."
    Thanks

    Tomexx.

  2. #2
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Format numbers

    Try the FormatNumber function it may help you

    Something like:

    VB Code:
    1. Msgbox FormatNumber(4.2512, 2)

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    Re: Format numbers

    Well,
    That will format 4 to 4.00
    I want 4 to stay 4 and 4.2512 to become 4.25
    Thanks

    Tomexx.

  4. #4
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Format numbers

    Quote 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:
    1. Private Function MyFormat(dblNumber As Double) As Double
    2.     If FormatNumber(dblNumber, 2) = dblNumber Then
    3.         MyFormat = FormatNumber(dblNumber, 0)
    4.     Else
    5.         MyFormat = FormatNumber(dblNumber, 2)
    6.     End If
    7. End Function

    Cheers,

    RyanJ
    Last edited by sciguyryan; Jun 2nd, 2005 at 10:10 AM.
    My Blog.

    Ryan Jones.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    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.
    Thanks

    Tomexx.

  6. #6
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Format numbers

    Quote 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
    My Blog.

    Ryan Jones.

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Format numbers

    Quote 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:
    1. FormatNumber([number], 2)

  8. #8
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Format numbers

    Quote Originally Posted by penagate
    Just use this
    VB Code:
    1. 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
    My Blog.

    Ryan Jones.

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Format numbers

    Sorry, I thought Tomexx wanted 4.00 instead of 4. Misunderstood.

    In that case,
    VB Code:
    1. CStr(Round([number], 2))
    The CStr is optional.

  10. #10
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Format numbers

    Cool, I never knew the Round function had a second parameter, thanks for the tip

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  11. #11
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Format numbers

    Quote 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

    Quote 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
    My Blog.

    Ryan Jones.

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